欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Call Bash using Python

程序员文章站 2022-07-14 13:29:08
...

Please have a look over here :)

http://*.com/questions/4256107/running-bash-commands-in-python

Mainly

vim /tmp/call.py

 

Don't use os.system. Use subprocess.

Like in your case:

#bashCommand ="cwm --rdf test.rdf --ntriples > test.nt"
bashCommand ="ls"
import subprocess
#process
= subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) # we need to split() the command if there are many flags
output
= process.communicate()[0]
process = subprocess.Popen(bashCommand, stdout = subprocess.PIPE)
print output # for python2
#print(output) # for python3+
python /tmp/call.py

 

You will get:

[email protected] /tmp $ python call.py 
call.py
CRX_75DAF8CB7768
fcitx-socket-:0
hsperfdata_alex
keyring-StERCr
mintUpdate
orbit-alex
pulse-2L9K88eMlGn7
pulse-PKdhtXMmr18n
untitled4897414270208832777.tmp

 

The output result is the same as the result gotten from BASH

 

转载于:https://www.cnblogs.com/spaceship9/archive/2013/04/15/3022600.html

上一篇: 目录拷贝–bash实现

下一篇: bash