Subprocess 当popen对dosen';不存在错误,没有返回结果

Subprocess 当popen对dosen';不存在错误,没有返回结果,subprocess,popen,communicate,Subprocess,Popen,Communicate,我正在尝试使用subprocess.Popen()读取服务状态 如果出现无法识别的维修错误,则打印到屏幕上的值不会保存到out或err,以供以后查看 如果服务确实存在,“正在运行”将保存到out值。当遇到无法识别的错误时,我希望获得相同的结果 我已尝试使用子流程。请检查输出,但无法识别 如果我使用现有的服务,它会按需要工作 我的代码: p = subprocess.Popen(['service','PretendService','status'], stdout=subprocess.PIP

我正在尝试使用
subprocess.Popen()
读取服务状态

如果出现
无法识别的维修错误
,则打印到屏幕上的值不会保存到
out
err
,以供以后查看

如果服务确实存在,“正在运行”将保存到
out
值。当遇到无法识别的错误时,我希望获得相同的结果

我已尝试使用
子流程。请检查输出,但无法识别

如果我使用现有的服务,它会按需要工作

我的代码:

p = subprocess.Popen(['service','PretendService','status'], stdout=subprocess.PIPE)

out,err = p.communicate()

您需要管道
stderr
以及
stdout

p = subprocess.Popen(['service','PretendService','status'], 
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = p.communicate()
# Now err will have your error message

... 这非常简单