2

I'm trying to run some external executable code from within Python and then make use of the output.The code I'm using takes an external file and returns a single number (the number of images encoded in that file). When I run from the command line, I see the following:

me@ubuntu:~/nist/hsfsys/bin$ ./nummis  /usr/local/hsfsys/data/by_class/4a/train_4a.mis 
3962

Where 3962 is a correct output as near as I can tell

However, when I try to use subprocess from within Python, I get the following error:

me@ubuntu:~/nist/hsfsys/bin$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.check_output(["./nummis","/usr/local/hsfsys/data/by_class/4a/train_4a.mis"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['./nummis', '/usr/local/hsfsys/data/by_class/4a/train_4a.mis']' returned non-zero exit status 32
>>> subprocess.call(["./nummis","/usr/local/hsfsys/data/by_class/4a/train_4a.mis"])
3962
32

How should I interpret this "non-zero exit status 32" ? If something is wrong, why don't I see it on the command line? If nothing is wrong, why is Python complaining & how can I get it to stop complaining?

2
  • The behavior you're seeing is as documented for the subprocess .check_call() function. I guess you have two major options: wrap your call in an exception handler, or use subprocess.Popen and supply your own handling (check_output() is a wrapper around some .Popen() code). Another, rather convoluted, way to do this would be to call ['/bin/sh', '-c', 'your_command your_args; exit 0'] to force the subprocess to return zero regardless of what your command returns. But that's sorta silly. (You could also re-write the "nummis" command to return EXIT_SUCCESS as appropriate) Commented Jul 5, 2012 at 6:10
  • @Jim Dennis Thanks. Using p=subprocess.Popen(.....,stdout=subprocess.PIPE) followed by z=p.communicate() gave me access to the output I wanted. Commented Jul 5, 2012 at 6:34

1 Answer 1

2

The command line only reports the exit status when explicitly asked for it.

After calling your program from the command line, try

echo $?

in order to show the exit status. If it shows 32 as well, it is the called program which is guilty. It doesn't properly return 0; or return EXIT_SUCCESS; in its main().

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

2 Comments

echo $? does show 32, which according to /usr/include/asm-generic/errno-base.h. means there is a broken pipe. Thanks.
@user1245262 This lookup is only valid if the program explicitly returns the value of errno. Otherwise, it is up to the program nummis itself what its returncodes mean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.