Python 2.7 Python 2.7如何重定向;子流程Popen“;将结果保存到文件中?
我是Python里的一个泰罗人,在这上面跑了一堵砖墙。有了附加的Python脚本,我能够从“ip.txt”文件中列出的多个设备中为“Ping_Fail”和“Ping_Success”生成所需的控制台输出。如何将结果重定向到单独的文本文件“Ping_Fail.txt”和“Ping_Success.txt”Python 2.7 Python 2.7如何重定向;子流程Popen“;将结果保存到文件中?,python-2.7,subprocess,io-redirection,Python 2.7,Subprocess,Io Redirection,我是Python里的一个泰罗人,在这上面跑了一堵砖墙。有了附加的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 devi
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'