Skip to main content
 首页 » 编程设计

python之在 python 中运行后续命令

2025年05月04日19del

我希望能够在 python 中调用两个(或更多)命令,但我想确保第一个在第二个开始之前完成。 subprocess 可以吗?例如:

subprocess.call("script1 input1 > out", shell=True) 

当该命令完成时:

subprocess.call("script2 out>out2", shell=True) 

或者有更好的方法吗?

请您参考如下方法:

对 subprocess.call 的调用会阻塞,直到命令完成。来自documentation :

Wait for command to complete, then return the returncode attribute.

同样等待完成的类似选项是 subprocess.check_call . call 和 check_call 的区别在于,后者针对非零返回码引发异常,而前者针对所有情况返回返回码。