grabbing return codes from os.system() call

Gregory Jorgensen gregj at pobox.com
Thu Mar 8 02:02:53 EST 2001


os.system returns the exit status. The low-order byte of the exit status is the
signal number that killed the process, and the high-order byte is the exit
status (if the signal number is 0). This only works under Unix; on Windows the
exit status is always 0.

768 = 0x0300 -- there's your 3 return value.

This is documented in the Python Library Docs, at:

http://www.python.org/doc/current/lib/os-process.html

You can separate the bytes in many ways. One simple technique:

x = os.system('./returncode')
signal = x & 0xFF
exitcode = (x >> 8) & 0xFF


In article <%wFp6.9481$dL4.126404 at vixen.cso.uiuc.edu>, Damian Menscher says...
>
>I'm new to Python (as in, my experience is essentially the tutorial),
>but I've already come up with an interesting question:
>
>How do I get the return code from an os.system call?  I would have
>expected I could do something like
>
>---returncode---
>#/bin/csh
>echo do stuff
>exit 3
>
>and then in my python program I could do
>
>print os.system('./returncode')

Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com



More information about the Python-list mailing list