I have a piece of code that takes in a user-provided path to an executable or batch file. I split the path to get the filename and path (tail and head), and use the head as the current working directory for the new process. If the path does not contain directories, it works fine. However, I get the following error if it does:
WindowsError: [Error 2] The system cannot find the file specified
Example code:
def poop(self, path_to_program):
head, tail = path.split(path_to_program)
if(head.startswith('"')):
head = head.strip('"')
if(tail.endswith('"')):
tail = tail.strip('"')
if(head<>''):
p = Popen(tail, cwd=head, shell=False)
else:
p = Popen(tail)
If path_to_program is not split and passed directly to Popen, it works fine.
Not sure what’s wrong.
From the documentation:
What’s happening is that
cwdis the directory where the executable is executed. It is NOT the directory where subprocess looks for the executable. What you might want is: