在 python 中运行后续命令 [英] Running subsequent commands within python
问题描述
我希望能够在 python 中调用两个(或更多)命令,但我想确保第一个在第二个开始之前完成.subprocess
可以实现吗?例如:
I would like to be able to call two (or more) commands within python but I would like to make sure that the first is finished before the second starts. Is that possible with subprocess
? For example:
subprocess.call("script1 input1 > out", shell=True)
当该命令完成时:
subprocess.call("script2 out>out2", shell=True)
或者有更好的方法吗?
推荐答案
对 subprocess.call
的调用会阻塞,直到命令完成.来自文档:
A call to subprocess.call
blocks until the command completes. From the documentation:
等待命令完成,然后返回returncode
属性.
Wait for command to complete, then return the
returncode
attribute.
同样等待完成的类似选项是subprocess.check_call代码>
.call
和 check_call
之间的区别在于,后者针对非零返回码引发异常,而前者针对所有情况返回返回码.
A similar option that also waits for completion is subprocess.check_call
. The difference between call
and check_call
is that the latter raises an exception for a nonzero return code, whereas the former returns the return code for all cases.
这篇关于在 python 中运行后续命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!