I need to start excel and open a file directly from python. Currently I am using:
import os
os.system('start excel.exe file.xls')
However I do not get desired result. I want to open a file from local destination (file is in the same folder with program), but this code open’s the file with same name in my home (user) directory and not from my program directory.
The problem is that the directory where the program is located is not used. The current working directory is. So you have to find out which directory your program is located in, which python conveniently prepared for you in:
and either change directory to it:
or give full path for the file you want to open
Note, that while Windows generally accept forward slash as directory separator, the command shell (
cmd.exe) does not, so backslash has to be used here.startis Windows-specific, so it’s not big problem to hardcode it here. More importantly note that Windows don’t allow"in file-names, so the quoting here is actually going to work (quoting is needed, because the path is quite likely to contain space on Windows), but it’s bad idea to quote like this in general!