20180918-第六章文件学习

18、有一个ip.txt,里面每行是一个ip,实现一个函数,ping 每个ip的结果,把结果记录存到ping.txt中,格式为ip:0或ip:1 ,0代表ping成功,1代表ping失败

ip.txt文件:

17.2.2.192
119.75.213.61
17.2.2.193
182.254.50.164
17.2.2.194
60.207.246.98
17.2.2.195
124.193.235.253
17.2.2.196
124.200.112.1

代码:
def ping(path,filename):
import os
import codecs
i_file_path=path+filename
with codecs.open(i_file_path,“r”,encoding=“utf-8-sig”) as f:
for line in f:
tmp='ping '+line
data=os.popen(tmp).read()
if “100% 丢失” in data:
ping_flag = “ping failed”
else:
ping_flag=“ping success”
o_file_path=path+“ping.txt”
with open(o_file_path,“a+”,encoding=“utf-8”) as outfile:
outfile.write("******************\n")
tmp2=tmp+":"+ping_flag
outfile.write(tmp2)
outfile.write(data+"\n")
outfile.write("******************\n")

ping(“c:\code\”,“ip.txt”)
print(“success”)

执行结果:
C:\Users\admin\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/admin/PycharmProjects/untitled1/test.py
success

Process finished with exit code 0

ping.txt 文件:


ping 17.2.2.192

:ping failed
正在 Ping 17.2.2.192 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

17.2.2.192 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),



ping 119.75.213.61

:ping success
正在 Ping 119.75.213.61 具有 32 字节的数据:
来自 119.75.213.61 的回复: 字节=32 时间=5ms TTL=56
来自 119.75.213.61 的回复: 字节=32 时间=7ms TTL=56
来自 119.75.213.61 的回复: 字节=32 时间=5ms TTL=56
来自 119.75.213.61 的回复: 字节=32 时间=9ms TTL=56

119.75.213.61 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 5ms,最长 = 9ms,平均 = 6ms



ping 17.2.2.193

:ping failed
正在 Ping 17.2.2.193 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

17.2.2.193 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),



ping 182.254.50.164

:ping success
正在 Ping 182.254.50.164 具有 32 字节的数据:
来自 182.254.50.164 的回复: 字节=32 时间=14ms TTL=53
来自 182.254.50.164 的回复: 字节=32 时间=11ms TTL=53
来自 182.254.50.164 的回复: 字节=32 时间=13ms TTL=53
来自 182.254.50.164 的回复: 字节=32 时间=12ms TTL=53

182.254.50.164 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 11ms,最长 = 14ms,平均 = 12ms



ping 17.2.2.194

:ping failed
正在 Ping 17.2.2.194 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

17.2.2.194 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),



ping 60.207.246.98

:ping success
正在 Ping 60.207.246.98 具有 32 字节的数据:
来自 60.207.246.98 的回复: 字节=32 时间=6ms TTL=58
来自 60.207.246.98 的回复: 字节=32 时间=7ms TTL=58
来自 60.207.246.98 的回复: 字节=32 时间=5ms TTL=58
来自 60.207.246.98 的回复: 字节=32 时间=6ms TTL=58

60.207.246.98 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 5ms,最长 = 7ms,平均 = 6ms



ping 17.2.2.195

:ping failed
正在 Ping 17.2.2.195 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

17.2.2.195 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),



ping 124.193.235.253

:ping success
正在 Ping 124.193.235.253 具有 32 字节的数据:
来自 124.193.235.253 的回复: 字节=32 时间=6ms TTL=113
来自 124.193.235.253 的回复: 字节=32 时间=6ms TTL=113
来自 124.193.235.253 的回复: 字节=32 时间=14ms TTL=113
来自 124.193.235.253 的回复: 字节=32 时间=5ms TTL=113

124.193.235.253 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 5ms,最长 = 14ms,平均 = 7ms



ping 17.2.2.196

:ping failed
正在 Ping 17.2.2.196 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

17.2.2.196 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),



ping 124.200.112.1

:ping success
正在 Ping 124.200.112.1 具有 32 字节的数据:
来自 124.200.112.1 的回复: 字节=32 时间=29ms TTL=58
来自 124.200.112.1 的回复: 字节=32 时间=8ms TTL=58
来自 124.200.112.1 的回复: 字节=32 时间=6ms TTL=58
来自 124.200.112.1 的回复: 字节=32 时间=8ms TTL=58

124.200.112.1 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 6ms,最长 = 29ms,平均 = 12ms


19、实现DOS命令执行功能,接受输入命令并执行,然后把执行结果和返回码打印到屏幕
import os
while 1:
command=input(“请输入DOS命令或E-退出键:”)
if command==“E”:
print(“退出程序!”)
break
else:
data=os.popen(command).read()
print("***************")
print(data)
print("***************")

执行结果:
C:\Users\admin\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/admin/PycharmProjects/untitled1/test.py
请输入DOS命令或E-退出键:dir


驱动器 C 中的卷是 OS
卷的序列号是 EC97-F6CA

C:\Users\admin\PycharmProjects\untitled1 的目录

2018/09/18 21:49

.
2018/09/18 21:49
2018/09/18 21:49 .idea
2018/09/18 21:49 287 test.py
2018/06/30 15:27 venv
1 个文件 287 字节
4 个目录 38,487,875,584 可用字节


请输入DOS命令或E-退出键:cd c:\code\



请输入DOS命令或E-退出键:dir


驱动器 C 中的卷是 OS
卷的序列号是 EC97-F6CA

C:\Users\admin\PycharmProjects\untitled1 的目录

2018/09/18 21:49

.
2018/09/18 21:49
2018/09/18 21:50 .idea
2018/09/18 21:49 287 test.py
2018/06/30 15:27 venv
1 个文件 287 字节
4 个目录 38,487,814,144 可用字节


请输入DOS命令或E-退出键:ver


Microsoft Windows [版本 10.0.16299.611]


请输入DOS命令或E-退出键:time


当前时间: 21:52:41.66
输入新时间:


请输入DOS命令或E-退出键:pwd



请输入DOS命令或E-退出键:‘pwd’ ???е???
???



请输入DOS命令或E-退出键:ping www.baidu.com


正在 Ping www.a.shifen.com [119.75.216.20] 具有 32 字节的数据:
来自 119.75.216.20 的回复: 字节=32 时间=11ms TTL=56
来自 119.75.216.20 的回复: 字节=32 时间=11ms TTL=56
来自 119.75.216.20 的回复: 字节=32 时间=8ms TTL=56
来自 119.75.216.20 的回复: 字节=32 时间=6ms TTL=56

119.75.216.20 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 6ms,最长 = 11ms,平均 = 9ms


请输入DOS命令或E-退出键:ping 17.2.2.193


正在 Ping 17.2.2.193 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

17.2.2.193 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),


请输入DOS命令或E-退出键:e
‘e’ ???е???
???



请输入DOS命令或E-退出键:E
退出程序!

Process finished with exit code 0

20、文件访问
访问一存在多行的文件,实现每隔一秒逐行显示文本内容的程序,每次显示文本文件的 5行, 暂停并向用户提示“输入任意字符继续”,按回车键后继续执行,直到文件末尾。
显示文件的格式为:
[当前时间] 一行内容,比如:[2016-07-08 22:21:51] 999370this is test

import codecs
import time

with codecs.open(“c:\code\abc.txt”, “r”, encoding=“utf-8-sig”) as f:
lines = f.readlines()

start_line = 0
for i in range(len(lines)):
if i % 5 == 0 and i != 0:
data = lines[start_line:i]
start_line = i
for j in data:
print(j.strip())
command = input(“请输入任意字符继续,不输入则需等10s:”)
print(“程序等待开始时间 : %s” % time.ctime())
if command == “”:
time.sleep(10)
print(“程序等待结束时间 : %s” % time.ctime())

elif i == len(lines)-1:
    data2 = lines[start_line:i]
    for k in data2:
        print(k.strip())
    print("文件读取完毕!")

执行结果:

C:\Users\admin\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/admin/PycharmProjects/untitled1/test.py
0 one two three four five!
1 one two three four five!
2 one two three four five!
3 one two three four five!
4 one two three four five!
请输入任意字符继续,不输入则需等10s:
程序等待开始时间 : Tue Sep 18 22:32:34 2018
程序等待结束时间 : Tue Sep 18 22:32:44 2018
5 one two three four five!
6 one two three four five!
7 one two three four five!
8 one two three four five!
9 one two three four five!
请输入任意字符继续,不输入则需等10s:6
程序等待开始时间 : Tue Sep 18 22:32:48 2018
程序等待结束时间 : Tue Sep 18 22:32:48 2018
10 one two three four five!
11 one two three four five!
12 one two three four five!
13 one two three four five!
14 one two three four five!
请输入任意字符继续,不输入则需等10s:6
程序等待开始时间 : Tue Sep 18 22:32:51 2018
程序等待结束时间 : Tue Sep 18 22:32:51 2018
15 one two three four five!
16 one two three four five!
17 one two three four five!
18 one two three four five!
19 one two three four five!
请输入任意字符继续,不输入则需等10s:
程序等待开始时间 : Tue Sep 18 22:32:53 2018
程序等待结束时间 : Tue Sep 18 22:33:03 2018
20 one two three four five!
21 one two three four five!
22 one two three four five!
23 one two three four five!
24 one two three four five!
请输入任意字符继续,不输入则需等10s:o
程序等待开始时间 : Tue Sep 18 22:33:06 2018
程序等待结束时间 : Tue Sep 18 22:33:06 2018
25 one two three four five!
26 one two three four five!
27 one two three four five!
28 one two three four five!
29 one two three four five!
请输入任意字符继续,不输入则需等10s:k
程序等待开始时间 : Tue Sep 18 22:33:09 2018
程序等待结束时间 : Tue Sep 18 22:33:09 2018
30 one two three four five!
31 one two three four five!
32 one two three four five!
33 one two three four five!
34 one two three four five!
请输入任意字符继续,不输入则需等10s:
程序等待开始时间 : Tue Sep 18 22:33:11 2018
程序等待结束时间 : Tue Sep 18 22:33:21 2018
35 one two three four five!
36 one two three four five!
37 one two three four five!
38 one two three four five!
39 one two three four five!
请输入任意字符继续,不输入则需等10s:
程序等待开始时间 : Tue Sep 18 22:33:25 2018
程序等待结束时间 : Tue Sep 18 22:33:35 2018
40 one two three four five!
41 one two three four five!
42 one two three four five!
文件读取完毕!

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值