I want to open a file using the subprocess
module as though the file was double-clicked in Explorer. How do I do that?
I tried the following line:
subprocess.call("C:/myfile.csv", shell=True)
which throws an error saying:
The syntax of the command is
incorrect.
'C:\' is not recognized as
an internal or external command,
operable program or batch file.
How do I emulate a double-click using subprocess
? Basically I want to open a CSV file in Excel 2007.
os.startfile(r'C:\myfile.csv')
(Win32 only. For Mac, run a process with 'open filename'
; on Linux/freedesktop-in-general, 'xdg-open filename'
.)
I think part of your problem is you're using a unix style slash / as a path separator, instead of the windows backslash . It looks like windows is interpreting /myfile.csv
as an argument for the program C:
, which is why you're getting that message.
However if you corrected that, I think you'd just get it saying that C:\myfile.csv
isn't a program.
I know that this is a bit late, but to do that in python 2.x (not sure about 3) you should use the subprocess
module, referencing Popen
. Here is the code:
import subprocess
subprocess.Popen(r'explorer /select, "C:\"')
It basically opens the file, and then opens it in the default program.