user3583419
user3583419

Reputation:

Python subprocess does not take correct arguments

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?

Upvotes: 0

Views: 1370

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148870

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)

Upvotes: 1

Related Questions