0

I am trying to automate some tasks - the process requires I call some exe's and pass parameters. The particular directories for the exe's are in the PATH variable for windows. However, I consistently get a

WindowsError: [Error 2] The system cannot find the file specified

My current workaround is to set the os.cwd to the directory with the exe but that imposes some other limits on how we distribute the code. I want to note that in every case if I start a cmd window and type the same code I am passing to subprocess.check_output the code works no matter what directory I am in on the computer.

Just to be clear I am afraid for example of trying to automate a WinRAR task and WinRAR.exe is in a different folder on their computer.

Okay in response to the comment below here is the input and the output after I changed the cwd to root (c:)

The call to subprocess

rarProcess = check_output('''WinRAR a -r -v700m -sfx -agYYYYMMDD-NN -iiconD:\\RarResources\\de96.ico -iimgd:\\RarResources\\accounting2013.bmp d:\\testFTP\\compressed_test_ d:\\files_to_compress''')

and here is the Traceback message in all of it's glory

Traceback (most recent call last):
File "<pyshell#93>", line 1, in <module>
rarProcess = check_output('''WinRAR a -r -v700m -sfx -agYYYYMMDD-NN -iiconD:\\RarResources\\de96.ico -iimgd:\\RarResources\\accounting2013.bmp d:\\testFTP\\compressed_test_ d:\\files_to_compress''')
 File "C:\Program Files (x86)\python\lib\subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Program Files (x86)\python\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Program Files (x86)\python\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Now I can't prove that this is not a hypothetical question/problem. I get the intended results when I use the same command (adjusting for path separators) through the cmd window and if I change the directory to the directory with the exe before running the command as pasted above.

PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
  • Check out this (http://stackoverflow.com/questions/5658622/python-subprocess-popen-environment-path) or this (http://stackoverflow.com/questions/14679466/subprocess-call-or-subprocess-popen-cannot-use-executables-that-are-in-path-lin) – sapi Sep 02 '14 at 23:36
  • Please show an actual code example, with its intended behaviour and actual output, rather than describe a hypothetical problem. I suspect that you have issues with files in other directories because you are not quoting the `\\` character properly in during literals. – Dan Lenski Sep 02 '14 at 23:38
  • `help(subprocess.check_output)` refers you to Popen. `help(subprocess.Popen)` tells you about the cwd parameter. People around here tend to get grumpy if you don't check help first. – tdelaney Sep 02 '14 at 23:58
  • Well I did try help check_output and I don't see anything that guided me. Thanks but it was not helpful – PyNEwbie Sep 03 '14 at 01:52

1 Answers1

1

You don't need to set os.cwd and run the process. Instead you pass the location of your "Winrar.exe" file to the subprocess as a dict.

proc = subprocess.Popen(args, env={'PATH': '/path/to/winrar.exe'})
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
  • Thanks I marked this as useful but it is not the answer I need because I might not know the path to the exe that I need, all I know is that the path is going to be one of the environment variables. – PyNEwbie Sep 03 '14 at 01:53