tags:

views:

625

answers:

4

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?

A: 

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.

Mike Hordecki
Just taking the quotes off the code above, It says 'blah' is not defined in my example
Tyler
+2  A: 

You need to properly escape the space in the executable path

David Cournapeau
escape the space?
Tyler
Yes, escape the space. Also, what's the "/" doing there? Doesn't windows use "\"? Generally, don't use "Program Files" or "My Documents" for anything, and you'll be much happier.
S.Lott
/ works just fine in system calls on Windows and did so even in real DOS.
Sinan Ünür
A: 

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.

Sinan Ünür
blah.exe is used in running the program. I'm feeding it a for statement with some numbers the blah.exe file uses.
Tyler
+1  A: 

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"

AlbertoPL
There should be two backslashes in front of Program Files, Stack overflow's editor is cutting one of them off.
AlbertoPL