有的时候需要执行下cmd, 然后获取cmd里输出的信息,可以使用如下方式
os获取
import os
t = os.popen('svn info {}')
var = t.read()
subprocess获取
一次性输出
p = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = p.wait()
if res != 0:
error_msg = p.communicate()[0]
print error_msg
分批输出
p = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,)
while p.poll() is None:
print p.stdout.readline()
结合
p = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while p.poll() is None:
print p.stdout.readline()
res = p.wait()
if res != 0:
error_msg = p.communicate()[0]
print error_msg