python新人,如何将结果输出到txt文件.
txt文件按日期时间自动生成,并将真假结果分别输入对应txt文件中.在网上找了很多例子最终还是没写成功.希望大神可以帮我完善一下代码.感谢🙏#coding...
txt文件按日期时间自动生成,并将真假结果分别输入对应txt文件中.在网上找了很多例子最终还是没写成功.希望大神可以帮我完善一下代码.感谢🙏#coding='utf-8'import os,sysfrom random import sampletruelist = []falselist = []IP = ['192.168.1.1','192.168.0.1','192.168.1.2']for i in sample(IP,2): ping = os.system('ping -c 3 %s'%i) if ping: falselist.append(i) else: truelist.append(i)print('truelist:' + str(truelist))print('falselist:' + str(falselist))
展开
1个回答
2019-01-22
展开全部
#coding='utf-8'
import os,sys
from random import sample
import time
import datetime
truelist = []
falselist = []
IP = ['192.168.1.1','192.168.0.1','192.168.1.2']
f = open(datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".txt", "w")
for i in sample(IP,2):
ping = os.system('ping -c 3 %s'%i)
if ping:
falselist.append(i)
else:
truelist.append(i)
print('truelist:' + str(truelist), file = f)
print('falselist:' + str(falselist), file = f)
f.close()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询