[英]Python. Code repeats several times because the command to execute it is different
[英]Tryin to do ping to several pc with python. .
我正在尝试读取一个文件,其中包含300台计算机的ips,然后写入一个新文件(如果存在或不存在)。
import os
import datetime
import platform
import subprocess
date = datetime.datetime.now()
day = date.day
hour = date.hour
os.chdir ('Path')
openips = open ("ips.txt","r")
ipfile = openips.readlines()
print (ipfile)
for ips in ipfile():
ips = ips.strip()
print (ips)
args = ["ping", "-n", "4", "-l", "1", "-w", "1000", ips]
pping = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
for line in pping.stdout:
print (line)
os.chdir ('Path')
presult = open ("pingresults_{}_{}.txt".format(day,hour), 'a')
presult.write ('{}_{}\n'.format(ips, line))
presult.close ()
Dunno为什么,每次我测试我的代码时。 。 。 结果是:
TypeError:“列表”对象不可调用
即使将变量更改为string,我也已尝试了所有方法。 。 。 说的一样,只是更改字符串列表
更改:
for ips in ipfile:
...
没有括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.