I need to be able to open a document using its default application in Windows and Mac OS. Basically, I want to do the same thing that happens when you double click on the document icon in Explorer or Finder. What is the best way to do this in Python?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
os.startfile(path, 'open') under windows is good because when spaces exist in the directory, os.system('start', path_name) can't open the app correct and when the i18n exist in the directory, os.system needs to change the unicode to the codec of the console in Windows.
If you have to use an heuristic method, you may consider
webbrowser
.It's standard library and despite of its name it would also try to open files:
I tried this code and it worked fine in Windows 7 and Ubuntu Natty:
This code also works fine in Windows XP Professional, using Internet Explorer 8.
on mac os you can call 'open'
this would open the file with TextEdit, or whatever app is set as default for this filetype
Use the
subprocess
module available on Python 2.4+, notos.system()
, so you don't have to deal with shell escaping.The double parentheses are because
subprocess.call()
wants a sequence as its first argument, so we're using a tuple here. On Linux systems with Gnome there is also agnome-open
command that does the same thing, butxdg-open
is the Free Desktop Foundation standard and works across Linux desktop environments.In Mac OS, you can use the "open" command. There is a Windows API call that does something similar, but I don't remember it offhand.
Update
Okay, the "start" command will do it, so this should work.
Mac OS/X:
Windows:
Much later update by Edward: os.system works, but it only works with filenames that don't have any spaces in folders and files in the filename (e.g. A:\abc\def\a.txt).
Later Update
Okay, clearly this silly-ass controversy continues, so let's just look at doing this with subprocess.
open
andstart
are command interpreter things for Mac OS/X and Windows respectively. Now, let's say we use subprocess. Canonically, you'd use:Now, what are the advantages of this? In theory, this is more secure -- but in fact we're needing to execute a command line one way or the other; in either environment, we need the environment and services to interpet, get paths, and so forth. In neither case are we executing arbitrary text, so it doesn't have an inherent "but you can type
'filename ; rm -rf /'
" problem, and IF the file name can be corrupted, usingsubprocess.call
gives us no protection.It doesn't actually give us any more error detection, we're still depending on the
retcode
in either case. We don't need to wait for the child process, since we're by problem statement starting a separate process."But
subprocess
is preferred." However,os.system()
is not deprecated, and it's the simplest tool for this particular job.Conclusion: using
os.system()
is the simplest, most straightforward way to do this, and is therefore a correct answer.