擅长:python、mysql、java
<p>你需要对过程的输出做些什么吗?</p>
<p>在这里,<code>check_call</code>方法可能很有用。请参见此处的python文档:<a href="https://docs.python.org/2/library/subprocess.html#subprocess.check_call">https://docs.python.org/2/library/subprocess.html#subprocess.check_call</a></p>
<p>然后可以按如下方式使用:</p>
<pre><code>try:
subprocess.check_call(command)
except subprocess.CalledProcessError:
# There was an error - command exited with non-zero code
</code></pre>
<p>但是,这依赖于<code>command</code>返回成功完成的退出代码0和错误的非零值。</p>
<p>如果还需要捕获输出,那么<code>check_output</code>方法可能更合适。如果您也需要,仍然可以重定向标准错误。</p>
<pre><code>try:
proc = subprocess.check_output(command, stderr=subprocess.STDOUT)
# do something with output
except subprocess.CalledProcessError:
# There was an error - command exited with non-zero code
</code></pre>
<p>请看这里的文档:<a href="https://docs.python.org/2/library/subprocess.html#subprocess.check_output">https://docs.python.org/2/library/subprocess.html#subprocess.check_output</a></p>