popen3 and return code
Question: I want to get stdout and stderr of a command along with the return code. Can
someone point me to a python function that already accomplishes this?

I modified a function that I found on this site as follows -- but I am not
able to grab the return code of the command. In this snippet, sts always seems
to be 0.

    
    
    def Getstatusoutput(cmd):
        """Return (status, output) of executing cmd in a shell."""
    
        import sys
        mswindows = (sys.platform == "win32")
    
        import os
        if not mswindows:
            cmd = '{ ' + cmd + '; }'
    
        fi,fo,fe=os.popen3(cmd)
        textOut = fo.read()
        textErr = fe.read()
        sts = fo.close()
        if sts is None: sts = 0
        if textOut[-1:] == '\n': textOut = textOut[:-1]
        return sts, textOut, textErr  
    


Answer: Use the [subprocess](http://docs.python.org/library/subprocess.html) module.
[This section](http://docs.python.org/library/subprocess.html#replacing-os-
popen-os-popen2-os-popen3) shows how to replace `os.popen3` calls.

