0

I am using Windows and 32-bit Python 2.7.

I have already read many posts that involves getting the subprocess module in Python work properly - making shell = True, single string vs. list of strings, using raw strings etc. What I am confused about is that the Python not only fails to produce an output of a program I am executing, but also fails to run some of the commands introduced in the documentation.

For instance, when I try to use "subprocess.check_call(["ls", "-l"])" as introduced in https://docs.python.org/2/library/subprocess.html#subprocess.check_call in python interactive console, it produce "WindowsError: [Error 2] The system cannot find the file specified".

Similarly, when I try to use "subprocess.call(["ls", "-l"])" as it appears exactly in the documentation, Python once again produces "WindowsError: [Error 2] The system cannot find the file specified" error. It only executes correctly and returns the exit status 1 if I use "subprocess.call(["ls", "-l"], shell = True)", different from what I read on the doc page.

More to the point, there is a Windows executable program which I wish to execute via Python. I have confirmed the program functions properly in the Cygwin terminal, it does not print any output when executed with Python (I noticed that this problem has been asked a few times, but the solutions did not work for me).

import subprocess as sub

rt = sub.Popen([r'C:/Users/Y L/Documents/ssocr', '-d', '-1', 'Sample.JPG'], stdin = sub.PIPE, stdout = sub.PIPE, stderr = sub.PIPE)
out, err = rt.communicate()
print(out, err)

When I print (out, err), this generates a tuple of an empty string pair. More interestingly, the program executes the same way and produces an identical output when the image file passed in is a total gibberish, which implies the arguments are not even being passed in properly.

import subprocess as sub

rt = sub.Popen([r'C:/Users/Y L/Documents/ssocr', '-d', '-1', 'asdfasdf.JPG'], stdin = sub.PIPE, stdout = sub.PIPE, stderr = sub.PIPE)
out, err = rt.communicate()
print(out, err)

Is there something I am missing about the arguments are handled by the subprocess module?

  • skip the r and flip the slashes? – Will Nov 06 '14 at 08:14
  • @Will That's one of the things I have tried before but no luck. Windows seems to take '/' or '\' fine, and 'r' was to recognize the spacing in the path correctly. –  Nov 06 '14 at 08:16
  • Is `Y L` the name of a directory? That is, the executable is `C:/Users/Y L/Documents/ssocr` rather than `C:/Users/Y` with a first argument `L/Documents/ssocr`. – Dunes Nov 06 '14 at 09:26
  • @Dunes Yes, it is the name of the directory. I thought using the raw string and separating each argument in a list instead of one string solves this problem. Am I mistaken? –  Nov 06 '14 at 09:28
  • That will work as expected. I was wondering if that might be the source of the error. – Dunes Nov 06 '14 at 09:29
  • Where are you running the python script from? It may be that the current working directory is not what you expect it to be, so `Sample.jpg` cannot be found. Try printing out `os.getcwd()` in your script. – Dunes Nov 06 '14 at 09:35
  • @Dunes hmm... it prints the cwd as 'C:/Users/Y L/Documents'. The script, executable program, and image are all placed within the same directory so the path should be correct. –  Nov 06 '14 at 09:51
  • If `sscor` exists in the cwd then you should be able to have the first arg as just `"sscor"`. I'm at a loss as to what is happening. I would write a script that just prints the arguments and call that in the subprocess so you can examine what the child process is actually receiving. – Dunes Nov 06 '14 at 10:01
  • @Dunes I just did what I suggested and learned that the arguments that I pass in as a list gets printed in the console as [\'arg1\', \'arg2\', \'arg3\', ...], including the escape sequences. Is this normal? –  Nov 06 '14 at 10:09
  • @Dunes I got the problem fixed. The problem involved the program not being able to execute in cmd window in the first place. Thanks for your input. –  Nov 06 '14 at 10:34

1 Answers1

1

ls is a command of Unix like systems and does not exist under Windows. An almost equivalent command would be cmd /c dir because dir is an internal command of cmd.

Under Windows, you could have better luck with first executing the command directly under a cmd windows, and then passing a single command line to Popen (and add cmd /c first if the command is a cmd internal command)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • So I should run the command 'C:/Users/Y L/Documents/ssocr -d -1 Sample.JPG' under cmd? How can I achieve that? Should I just include 'cmd' as the first argument of Popen and add the subsequent arguments to it, under one Popen call? –  Nov 06 '14 at 09:41
  • Actually passing 'cmd' as the first element in the argument list only makes the terminal prompt to be piped as output, but the program output is still missing. Am I interpreting what you suggested correctly? –  Nov 06 '14 at 09:48
  • What I suggested was to first open a `cmd` window and in that window type the command to see what happens. I'd rather separate the problems of command syntax from the problems of executing from Python. – Serge Ballesta Nov 06 '14 at 10:01
  • I see. It gives the error "The program can't start because cygwin1.dll is missing from your computer. Try reinstalling the program to fix this problem." Do you think this could be why nothing was printing as the output or error? –  Nov 06 '14 at 10:20
  • @user3583419 Definitevely yes. The message is printed by `cmd.exe` because the program cannot be started. That's the reason why I advised you to first start the program *by hand*. – Serge Ballesta Nov 06 '14 at 10:28
  • Actually, I got the command to work in the cmd window by adding the path to the environment variable, but the call from the Python script is still not printing anything. The 'print(out, err)' is still a tuple of an empty string pair. –  Nov 06 '14 at 10:29
  • Never mind. I closed the current Python session and console and now it works. Guess it needs to be restarted for the path to take effect. Thank you so much. –  Nov 06 '14 at 10:31