格式化输出-python
我这里有这个密码格式化输出-python,python,Python,我这里有这个密码 import subprocess from time import strftime # Load just the strftime Module from Time f = open('check_'+strftime("%Y-%m-%d")+'.log', 'w') for server in open('check.txt'): f.write(server.strip() + "\n") subprocess.Popen(['plink
import subprocess
from time import strftime # Load just the strftime Module from Time
f = open('check_'+strftime("%Y-%m-%d")+'.log', 'w')
for server in open('check.txt'):
f.write(server.strip() + "\n")
subprocess.Popen(['plink', server.strip(), 'df','-k'],stdout=f)
我想要的是输出,因此它显示:
服务器名
输出
服务器名
输出
目前显示:
服务器名
服务器名
输出
输出
首先,请提前感谢如果您打算在远程服务器上运行不同的命令,请查看 默认情况下,subprocess.Popen在后台运行,并且您的命令可能比循环中的其他事情(例如打印服务器名称)慢得多。尝试此操作以强制它等待每个进程:
import subprocess
from time import strftime # Load just the strftime Module from Time
f = open('check_'+strftime("%Y-%m-%d")+'.log', 'w')
for server in open('check.txt'):
f.write(server.strip() + "\n")
p = subprocess.Popen(['plink', server.strip(), 'df','-k'],stdout=f)
p.wait()
f.flush()
如果您想让进程并行运行,我只需将每个服务器日志写入不同的文件。如果需要,请连接结果。非常好,非常感谢,它解决了这个问题。在输出后打印服务器时,我更改了f.flush()和p.wait()循环。