1
我想获得一个命令的stdout和stderr以及返回码。 有人可以指向我已经完成了这一点的python函数吗?popen3和返回码
我修改了一个我在本网站上找到的函数,如下所示 - 但我无法获取该命令的返回码。在这段代码中,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