Call Bash using Python

Please have a look over here :)

http://stackoverflow.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:

alex@universe /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/p/3022600.html