Question

I'm working on a bit of code that is supposed to run an exe file inside a folder on my system and getting an error saying...

WindowsError: [Error 3] The system cannot find the path specified. Here's a bit of the code:

exepath = os.path.join(EXE file localtion)
exepath = '"' + os.path.normpath(exepath) + '"'
cmd = [exepath, '-el', str(el), '-n', str(z)]

print 'The python program is running this command:'
print cmd

process = Popen(cmd, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]

I have imported subprocess and also from subprocess import *

For example, This is how my exe file location looks like in the first line of the code I show:

 exepath= os.path.join('/Program Files','next folder','next folder','blah.exe')

Am I missing something?

Was it helpful?

Solution

You need to properly escape the space in the executable path

OTHER TIPS

Besides properly escaping spaces and other characters that could cause problems (such as /), you can also use the 8 character old DOS paths.

For example, Program Files would be:

Progra~1 , making sure to append ~1 for the last two characters.

EDIT: You could add an r to the front of the string, making it a raw literal. Python would read the string character for character. Like this:

r " \Program files"

If I remember correctly, you don't need to quote your executuable file path, like you do in the second line.

EDIT: Well, just grabbed nearby Windows box and tested this. Popen works the same regardless the path is quoted or not. So this is not an issue.

AFAIK, there is no need to surround the path in quotation marks unless cmd.exe is involved in running the program.

In addition, you might want to use the environment variable ProgramFiles to find out the actual location of 'Program Files' because that depends on regional settings and can also be tweaked using TweakUI.

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