1.程序制作原因:
? ? ? ? 需要对IP文件进行IP去重和A、B、C段统计
2.最终效果图:
????????
3.完整代码如下(count_A_B_C.py):?
import os
import PySimpleGUI as sg
def count_d(iplist):
# print(iplist)
iplist = set(iplist)
# print(iplist)
return len(iplist)
def count_c(iplist):
# print(iplist)
c_list = []
for ip in iplist:
ip_arry = ip.split('.')
# print(ip_arry)
ip = '%s.%s.%s' % (ip_arry[0], ip_arry[1], ip_arry[2])
# print(ip)
c_list.append(ip)
# print(c_list)
c_list = set(c_list)
# print(c_list)
return len(c_list)
def count_b(iplist):
# print(iplist)
b_list = []
for ip in iplist:
ip_arry = ip.split('.')
# print(ip_arry)
ip = '%s.%s' % (ip_arry[0], ip_arry[1])
# print(ip)
b_list.append(ip)
# print(b_list)
b_list = set(b_list)
# print(b_list)
return len(b_list)
def count_a(iplist):
pass
# print(iplist)
a_list = []
for ip in iplist:
ip_arry = ip.split('.')
# print(ip_arry)
ip = '%s' % (ip_arry[0])
# print(ip)
a_list.append(ip)
# print(a_list)
a_list = set(a_list)
# print(a_list)
return len(a_list)
def read_iplist():
iplist = []
dir = os.getcwd()
dir = os.path.join(dir, "ip_result.txt")
if os.path.isfile(dir):
with open(dir, 'r') as f:
line = f.readline()
while line:
line = line.strip('\n')
if line:
iplist.append(line)
line = f.readline()
if not line:
break
# iplist = f.readlines()
# print(iplist)
else:
result = '当前目录不存在ip_result.txt文件,请创建后运行'
sg.popup_scrolled(result, title="错误信息")
exit()
return iplist
if __name__ == '__main__':
iplist = read_iplist()
ip_d = count_d(iplist)
# print(ip_d)
ip_c = count_c(iplist)
# print(ip_c)
ip_b = count_b(iplist)
# print(ip_b)
ip_a = count_a(iplist)
# print(ip_a)
# print('A段:%s B段:%s C段:%s D段:%s' %(ip_a,ip_b,ip_c,ip_d))
result = 'A段:%s B段:%s C段:%s D段:%s' % (ip_a, ip_b, ip_c, ip_d)
sg.popup_scrolled(result, title="IP统计结果")
# read_iplist()
4.程序打包成exe:
(venv) D:\work\venv\Scripts>pyinstaller.exe -Fw D:\work\count_A_B_C.py
5.程序运行注意事项:
? ? ? ? ①ip_result.txt文件需要跟打包成的exe文件放在同一目录下
? ? ? ? ②ip_result.txt文件中ip一行一个
|