2

I am trying to open a file on mac in python using eclipse.

FileName = "/Users/fis/Desktop/installation_guide.txt"
ss = subprocess.Popen(FileName, shell=True)
ss.communicate()

And also os.popen(FileName). But file is not opening. These codes work fine in windows. But, I don't know whats the problem with mac. I want to open a file just like double-clicking on windows to open a file and not like reading the content of file and printing in console. File is present on Desktop location on mac

falsetru
  • 357,413
  • 63
  • 732
  • 636
Harish Barvekar
  • 83
  • 2
  • 2
  • 7
  • possible duplicate of [How to open a file on mac OSX 10.8.2 in python](http://stackoverflow.com/questions/19273210/how-to-open-a-file-on-mac-osx-10-8-2-in-python) – Ben Oct 10 '13 at 14:08
  • 1
    So, you didn't get the answer you wanted in the first question so you just asked again? – KevinDTimm Oct 10 '13 at 15:23
  • @KevinDTimm yes file is still not opening. I tried a lot. Do not know whats the problem with it – Harish Barvekar Oct 10 '13 at 18:09
  • related: [Open document with default application in Python](http://stackoverflow.com/q/434597/4279), see also [my answer](http://stackoverflow.com/a/15055133/4279). The way *to wait for the application to exit/file to be closed* might depend on the application/OS that is used to open the file i.e., you might need specialized solutions for each file type. – jfs Oct 14 '13 at 08:30

1 Answers1

9

Use open (1) command.

import subprocess
FileName = "/Users/fis/Desktop/installation_guide.txt"
subprocess.call(['open', FileName])
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • ss=subprocess.Popen("open '/Users/fis/Desktop/installation_guide.txt'", shell=True) ss.wait(). File is opening but , it is not blocking. I tried with ss.communicate() also. I appreciate your suggestions – Harish Barvekar Oct 11 '13 at 04:10
  • I removed, wait/ communicate(). But it is still not blocking. – Harish Barvekar Oct 11 '13 at 04:42
  • @HarishBarvekar, I misunderstood your comment. Try `subprocess.call(['open', '-W', FileName])` – falsetru Oct 11 '13 at 04:48
  • subprocess.call(['open', '-W', FileName]) opens the file. But control is lost after its execution. I want is open a file. Then wait till file IS OPENED i.e. control should wait. And after the opened file is closed, the control is given to next statement. – Harish Barvekar Oct 11 '13 at 05:00
  • 1
    @HarishBarvekar, Using `-W` option, controll will be back to the python process once the opened program quit. – falsetru Oct 11 '13 at 05:54
  • I really appreciate your suggestions. I tried with ss=subprocess.call(['open', '-W', FileName]). File is opened and when I click on red cancel button left side, file deletes. But control is not passed to next instruction till, I quit text editor from dock. Now the issue is, I previously open a file, text editor now appears on dock. Then I again run the code, file opens. But the issue is, control will go to next instruction only when I quit the text editor from dock. In that case, I have to close the previous open file also. – Harish Barvekar Oct 11 '13 at 06:13
  • @HarishBarvekar, I have no idea. Maybe you need to use AppleScript to fully control the other application. – falsetru Oct 11 '13 at 06:28
  • I have no idea about apple script. Please help me in this. – Harish Barvekar Oct 11 '13 at 06:33
  • @HarishBarvekar, I neither am not goot at AppleScript. How about post another question (with tag `applescript ui-automation`) ? – falsetru Oct 11 '13 at 07:39