在 python 代码中,如何运行 .sh 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4826686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inside python code, how do I run a .sh script?
提问by TIMEX
Will it continue the code after it's run? Or will it stop at that line until the script is done?
运行后会继续执行代码吗?或者它会在该行停止直到脚本完成?
采纳答案by ThiefMaster
Using subprocess.callis the easiest way. It will not return until the executed program has terminated. Have a look at the other methods of the subprocess moduleif you need different behaviour.
使用subprocess.call是最简单的方法。在执行的程序终止之前它不会返回。如果您需要不同的行为,请查看subprocess 模块的其他方法。
回答by Frost.baka
import os
os.system('./script.sh')
python script won't stop until sh is finished
python脚本在sh完成之前不会停止
回答by Senthil Kumaran
You can use os.systemor subprocess.Popenor subprocess.callbut when using subprocess methods make sure you use shell=True. And executing it via system call in all these methods is blocking. The python script will complete and then go the next step.
您可以使用os.systemorsubprocess.Popen或subprocess.call但是在使用子流程方法时请确保使用shell=True. 在所有这些方法中通过系统调用执行它是阻塞的。python 脚本将完成,然后进行下一步。

