您可以使用名为[docs]subprocess的属性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