grabbing return codes from os.system() call

Pat Knight nospam at ktgroup.co.uk
Thu Mar 8 05:18:10 EST 2001


Damian Menscher <menscher+python at uiuc.edu> writes:

> 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')
> 
> 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

There's no byte swapping. UNIX shifts exit codes up 8 bits to include some
other flags about how a child process exited - normally, seg violation etc.

Look at the process management section of the os module documentation in
Python. This contains functions to decode exit codes.

Also, check the UNIX man pages for system, and the exec, exit and waitpid
system calls. You'll find details of the encoding, and probably find that you
can't have the large exit status values you want.

If you're wanting to return strings or large numbers, start investigating
os.popen.

Cheers,
        Pat



More information about the Python-list mailing list