Call Bash using Python

本文介绍了一种在Python中执行Bash命令的方法,并对比了使用os.system与subprocess模块的不同。通过具体示例展示了如何利用subprocess.Popen来运行简单的Bash命令并获取其输出。
摘要由CSDN通过智能技术生成

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/archive/2013/04/15/3022600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值