您需要对流程的输出做任何事情吗?
该
check_call方法在这里可能有用。在这里查看python文档:https
:
//docs.python.org/2/library/subprocess.html#subprocess.check_call
然后,您可以按以下方式使用它:
try: subprocess.check_call(command)except subprocess.CalledProcessError: # There was an error - command exited with non-zero pre
但是,这依赖于
command返回成功的退出代码0和成功的非零值。
如果还需要捕获输出,则该
check_output方法可能更合适。如果您也需要重定向标准错误,则仍然可以。
try: proc = subprocess.check_output(command, stderr=subprocess.STDOUT) # do something with outputexcept subprocess.CalledProcessError: # There was an error - command exited with non-zero pre
请参阅以下文档:https
:
//docs.python.org/2/library/subprocess.html#subprocess.check_output



