popen exit status

Mark McEahern marklists at mceahern.com
Mon Jan 28 11:59:43 EST 2002


david:
> the documentation says that popen returns the error code, or
> 'None', of the
> shell process. this code however always returns '1' even though
> the command
> operates fine.

I think popen returns the error status; I use this to derive the error code:

import os

def getExitCode(fileObject):
    """Close the file object and return the exitCode."""
    exitStatus = fileObject.close()
    exitCode = None
    if exitStatus:
        exitCode = os.WIFEXITED(exitStatus) and os.WEXITSTATUS(exitStatus)
or 0
    return exitCode

def runCommand(command):
    """Run the command and return the output and exit code."""
    f = os.popen(command)
    output = ''.join(f.readlines()).strip()
    exitCode = getExitCode(f)
    return output, exitCode

Cheers,

// mark





More information about the Python-list mailing list