Python 2.7如何将“subprocess.Popen”结果重定向到文件?

时间:2017-08-15 15:10:55

标签: python-2.7 subprocess io-redirection

我是Python中的一个tyro并在此上运行了一堵砖墙。附加了Python脚本后,我能够从“ip.txt”文件中列出的多个设备为“Ping_Fail”和“Ping_Success”生成所需的控制台输出。如何将结果重定向到单独的文本文件“Ping_Fail.txt”和“Ping_Success.txt”。

import subprocess
with open('ip.txt') as k: 
  devices = k.read().splitlines()
  for host in devices: 
        result=subprocess.Popen(['ping', '-c', '1', '-n', '-W', '2', host], 
            stdout=k, stderr=k).wait()
        if result:
          print host, 'Ping_Fail'
        else:
          print host, 'Ping_Success'   

0 个答案:

没有答案