您的位置:首页 > 运维架构 > Shell

python中执行shell命令

2013-07-29 17:24 393 查看
def run_command(self, cmd):
try:
import subprocess
except ImportError:
# Python 2.3
_, rf, ef = os.popen3(cmd)
else:
# Python 2.4+
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rf, ef = p.stdout, p.stderr
errors = ef.read()
if errors:
print("ERROR: %s" % errors)
return rf.read().strip()


该函数返回shell命令的执行结果,使用前需要先执行import os
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: