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
本文介绍了一个Python函数,用于执行Shell命令并返回其执行结果。适用于Python2.3及更高版本。

被折叠的 条评论
为什么被折叠?



