grabbing return codes from os.system() call

Grant Edwards grante at
Thu Mar 8 17:18:44 CET 2001

In article <%wFp6.9481$dL4.126404 at>, Damian Menscher wrote:

>How do I get the return code from an os.system call?  I would
>have expected I could do something like
>echo do stuff
>exit 3
>and then in my python program I could do
>print os.system('./returncode')
>But it prints out 768.  Not particularly useful, even after I
>recognize the trick of dividing by 256 (byte-swapping going on?
>No, because a return code of 768 reports as 0).

A return code of 0x0300 (768) means that the exit status was
0x03, and the signal number was 0x00.

>Given that my real return codes will be (possibly large)

They won't be larger than 255.  At least not on any Unix system
I've ever heard of.

>this limitation will likely cause some serious problems down
>the line.
>Even better would be a way of returning a string (the script I
>run can be something other than csh, but it has to be a
>separate script).

Take a look at the "commands" module. The function
commands.getstatusoutput("foobar") will return a tuple of exit
status, plus stdout and stderr strings.  It's just a wrapper
around popen2 routines, so you could do the same thing
yourself. Last time I looked it was only available on Unix

Grant Edwards                   grante             Yow!  Bo Derek ruined
                                  at               my life!

More information about the Python-list mailing list