There are loads of different solutions, and the results will strongly depend on:
- the OS you are using: Windows, Cygwin, Linux, MacOS
- the python version you are using: Python2 or Python3x
As I have discovered some things that are claimed to work only in Windows, doesn't, probably because I happen to use Cygwin which is outsmarting the OS way to deal with Windows paths. Other things only work in pure *nix based OS's or in Python2 or 3.
Here are my findings:
- Generally speaking,
os.system()
is the most forgiving method.
os.startfile()
is the least forgiving. (Windows only && if you're lucky)
subprocess.Popen([...])
not recommended
subprocess.run(winView, shell=True)
the recommended way!
- Remembering that using
subprocess
for anything may pose a security risk.
Try these:
import os, subprocess
...
winView = '/cygdrive/c/Windows/explorer.exe %s' % somefile
...
# chose one of these:
os.system(winView)
subprocess.Popen(['/cygdrive/c/Windows/explorer.exe', 'somefile.png'])
subprocess.run(winView, shell=True)
Q: Why would you want to use explorer
in Windows?
A: Because if you just want to look at the results of some new file, explorer will automatically open the file with whatever default windows program you have set for that file type. So no need to re-specify the default program to use.