calling bash from python

懵懂的女人 提交于 2019-12-01 12:59:32

If you use subprocess (as you should!) and don't specify stdin/stdout/stderr, they'll operate normally. For example:

# lol.py
import subprocess
p = subprocess.Popen(['cat'])
p.wait()

$ python lol.py
hello
hello
catting
catting

You could also process stdout as you like:

# lol.py
import subprocess

p = subprocess.Popen(['cat'], stdout=subprocess.PIPE)
p.wait()

print "\n\nOutput was:"
print p.stdout.read()

$ python lol.py
hello
catting

^D

Output was:
hello
catting

Try the envoy library.

import envoy
r = envoy.connect('./myBashScript')
r.send('input text') # send info on std_in
r.expect('output text') # block until text seen
print r.std_out # print whatever is in the std_out pipe

You can use os.system open a terminal window for that, such as gnome-terminal, to run the bash script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!