从Python运行bash脚本

4
我需要从Python运行一个bash脚本。我已经按照以下方式使其正常工作:
import os
os.system("xterm -hold -e scipt.sh")

这不完全是我正在做的事情,但基本上是这个想法。这很好用,一个新的终端窗口会打开,并且我会保持它以进行调试,但我的问题是,即使那个窗口没有完成,我仍然需要python脚本继续运行。有什么办法可以实现这一点吗?

1个回答

8
我建议您使用subprocess模块:文档 你可以:
import subprocess

cmd = "xterm -hold -e scipt.sh"
# no block, it start a sub process.
p = subprocess.Popen(cmd , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# and you can block util the cmd execute finish
p.wait()
# or stdout, stderr = p.communicate()

更多信息,请阅读文档:)。

修正拼写错误


工作得很完美。谢谢! - ajk4550

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接