Python学习总结10:获取shell输出结果
Python中获取shell命令的输出结果的常见方法如下几种:
1.
import subprocess output = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE,shell=True).communicate() print output[0]
2.
import commands return_code, output = commands.getstatusoutput('ls -l')
可返回状态与调用的shell命令的输出结果
3.
import os process = os.popen('ls -l') # return file output = process.read()
process.close()