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

4
  • possible duplicate of How to open a file on mac OSX 10.8.2 in python Commented Oct 10, 2013 at 14:08
  • 1
    So, you didn't get the answer you wanted in the first question so you just asked again? Commented Oct 10, 2013 at 15:23
  • @KevinDTimm yes file is still not opening. I tried a lot. Do not know whats the problem with it Commented Oct 10, 2013 at 18:09
  • related: Open document with default application in Python, see also my answer. 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. Commented Oct 14, 2013 at 8:30

1 Answer 1

10

Use open (1) command.

import subprocess
FileName = "/Users/fis/Desktop/installation_guide.txt"
subprocess.call(['open', FileName])
Sign up to request clarification or add additional context in comments.

9 Comments

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
I removed, wait/ communicate(). But it is still not blocking.
@HarishBarvekar, I misunderstood your comment. Try subprocess.call(['open', '-W', FileName])
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.
@HarishBarvekar, Using -W option, controll will be back to the python process once the opened program quit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.