我需要从 Python 运行 bash 脚本。我让它按如下方式工作:
import os
os.system("xterm -hold -e scipt.sh")
这不完全是我在做的,但几乎是我的想法。这工作正常,一个新的终端窗口打开,我为了调试目的保留它,但我的问题是我需要 python 脚本继续运行,即使它没有完成。我有什么办法可以做到这一点吗?
最佳答案
我推荐你使用subprocess
模块:docs
你可以
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()
有关更多信息,请阅读文档,:)。
编辑拼写错误
关于python - 从 Python 运行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20415522/