我正在使用这样的子流程
args = ['commandname', 'some args']
subprocess.check_output(args)
有时我会收到此错误
command returned non-zero exit status 1
有没有办法,如果获得非零退出状态,那么系统会使用该消息引发异常,如
output = subprocess.check_output(args)
if non zero exit :
raise Exception(errormessage)
答案 0 :(得分:0)
您可以使用名为subprocess
[docs]
returncode
属性
Popen.returncode
The child return code, set by poll() and wait() (and indirectly by communicate()).
A None value indicates that the process hasn’t terminated yet.
A negative value -N indicates that the child was terminated by signal N (Unix only).
所以它应该像这样工作(没有经过测试的代码) -
import subprocess
args = ['commandname', 'some args']
child = subprocess.Popen(args, stdout=subprocess.PIPE)
streamdata = child.communicate()[0]
returncode = child.returncode
if returncode != 0:
raise Exception