Python捕获命令发送到unix,捕获输出并写入文件

时间:2015-04-29 16:34:29

标签: python file unix command output

import subprocess 
cmd = 'ifconfig -a'

p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)

while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
       <<<HOW TO CMD SENT AND IT'S OUTPUT>>>
        file = open('outputFile.txt', 'w+')
        file.write(out)
        file.close()

0 个答案:

没有答案