0

我正在使用这样的子流程

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)
4

1 回答 1

0

您可以使用名为[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
于 2013-05-01T01:41:28.817 回答