popen3和返回代码
我想要获取一个命令的标准输出和错误输出,以及它的返回代码。有没有人能告诉我一个已经实现这个功能的Python函数?
我修改了一个我在这个网站上找到的函数,结果如下——但是我无法获取到命令的返回代码。在这个代码片段中,sts似乎总是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