从 Python 运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20415522/
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
Running a bash script from Python
提问by ajk4550
I need to run a bash script from Python. I got it to work as follows:
我需要从 Python 运行 bash 脚本。我让它工作如下:
import os
os.system("xterm -hold -e scipt.sh")
That isn't exactly what I am doing but pretty much the idea. That works fine, a new terminal window opens and I hold it for debugging purposes, but my problem is I need the python script to keep running even if that isn't finished. Any way I can do this?
这不完全是我在做什么,但几乎是我的想法。这工作正常,一个新的终端窗口打开,我拿着它进行调试,但我的问题是我需要 python 脚本继续运行,即使它没有完成。我有什么办法可以做到这一点?
回答by atupal
I recommend you use subprocess
module: docs
我建议您使用subprocess
模块:docs
And you can
你可以
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()
For more info, read the docs,:).
有关更多信息,请阅读文档,:)。
edited misspellings
已编辑的拼写错误