commands.getstatusoutput result is not command line exit value!!!

Fredrik Lundh fredrik at
Mon Oct 2 18:32:59 CEST 2006

Hari Sekhon wrote:

> I'm sorry, this may seem dense to you but I have to ask. What on earth 
> are you talking about?
> Why is it shifted 8 bits to the left? Why is there bitshifting at all? 
> Why doesn't commands give the same exit value as os.system() and the 
> unix cli?

because that's how Unix's wait() operation returns the status code (as 
mentioned in the "commands" section of the library reference).

you can use the os.WIFEXITED(status) and os.WEXITSTATUS(code) helpers to 
convert between wait return codes and signal numbers/exit codes.  see:

or you can forget about the obsolete "commands" module, and use the new 
subprocess module instead; e.g.

def getstatusoutput(command):
     from subprocess import Popen, PIPE, STDOUT
     p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
     s =
     return p.wait(), s

print getstatusoutput("ls -l /bin/ls")
(0, '-rwxr-xr-x    1 root     root        68660 Aug 12  2003 /bin/ls\n')

the subprocess module is highly flexible; see the library reference for 


More information about the Python-list mailing list