Question

I have made a very very simple script in python:

import os


path = str(os.getcwd())+'\\'+'INSTALLER PYTHON'+'\\'+'python-2.7.6.msi'
os.startfile(path)

Then, I have made an .exe file of it, and when I try to run it in windows I get this error:

Traceback <most recent call last>:
    File"<string>", line 12, in <module>
WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Users\\Desktop\\TestFolder\\INSTALLER PYTON\\python-2.7.6.msi'

BUT....in Spyder, if I do like this:

import os


path = str(os.getcwd())+'\\'+'INSTALLER PYTHON'+'\\'+'python-2.7.6.msi'
print path
#os.startfile(path)

I get this nice output:

'C:\Users\Desktop\TestFolder\INSTALLER PYTON\python-2.7.6.msi'

Which is exactly what I want so...

what is wrong with the backslash and the double backslash?

Was it helpful?

Solution

Use subprocess.

import subprocess

subprocess.call([path,arguments1,arguments2,arguments3,...])

Note that arguments are optional. If your executable requires/takes commandline arguments as input, you can pass them in argument field.

For more info: DOCS

You can also use os.system() however subprocess is prefered

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top