简体   繁体   English

在python代码中,如何运行.sh脚本?

[英]Inside python code, how do I run a .sh script?

Will it continue the code after it's run? 它会在运行后继续运行吗? Or will it stop at that line until the script is done? 或者它会在脚本停止直到脚本完成?

Using subprocess.call is the easiest way. 使用subprocess.call是最简单的方法。 It will not return until the executed program has terminated. 在执行的程序终止之前,它不会返回。 Have a look at the other methods of the subprocess module if you need different behaviour. 如果需要不同的行为,请查看子进程模块的其他方法。

import os
os.system('./script.sh')

python script won't stop until sh is finished 在sh完成之前,python脚本不会停止

You can use os.system or subprocess.Popen or subprocess.call but when using subprocess methods make sure you use shell=True . 您可以使用os.systemsubprocess.Popensubprocess.call但在使用子方法确保您使用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. python脚本将完成,然后进入下一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM