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?

10
  • skip the r and flip the slashes? Commented Nov 6, 2014 at 8: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. Commented Nov 6, 2014 at 8: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. Commented Nov 6, 2014 at 9: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? Commented Nov 6, 2014 at 9:28
  • That will work as expected. I was wondering if that might be the source of the error. Commented Nov 6, 2014 at 9:29

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

7 Comments

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?
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?
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.
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?
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.