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?
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.
Okay, the "start" command will do it, so this should work.
Okay, clearly this silly-ass controversy continues, so let's just look at doing this with subprocess.
start are command interpreter things for Mac OS/X and Windows respectively. Now, let's say we use subprocess. Canonically, you'd use:
try: retcode = subprocess.call("open " + filename, shell=True) if retcode < 0: print >>sys.stderr, "Child was terminated by signal", -retcode else: print >>sys.stderr, "Child returned", retcode except OSError, e: print >>sys.stderr, "Execution failed:", e
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, using
subprocess.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.
subprocess is preferred." However,
os.system() is not deprecated, and it's the simplest tool for this particular job.
os.system() is the simplest, most straightforward way to do this, and is therefore a correct answer.