[英]How to save the output of os.system to .csv file?
在写这篇文章之前,我试图自己做,但放弃了。
这是向我们显示延迟数据的代码:
import os
x = os.system("ping 192.168.1.1")
output 是:
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.47 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.97 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=3.02 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=2.74 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=2.74 ms
64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=2.08 ms
--- 192.168.1.1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5007ms
rtt min/avg/max/mdev = 2.087/2.674/3.020/0.319 ms
利用:
import os
x = os.popen('ping 198.168.1.1').read()
print (x)
分配给一个变量。
编辑:
这是完整的代码:
import os
ip = "192.168.1.1"
x = os.popen("ping "+ip).read()
print (x)
x = x.splitlines()
x = x[len(x)-1]
x = x.replace("rtt min/avg/max/mdev = ","")
x = x.replace(" ms" , "")
x = x.replace("/" , ",")
x = ip +","+ x
file = open("newfile.csv","w")
file.write(x)
print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.