文件操作练习题

生成一个ips.txt文件在其中保存120000个ip

import random
f= open('/tmp/ips.txt','a+')

for i in range(1200000:
f.write('172.25.254.' + str(random.randint(0,255) + '\n')

f.close( )

输出ips.txt文件中ip出现前十位的ip

f = open('/tmp/ips.txt','a+')
dic = { }
f.seek(0,0)
for i in f:
    dic[i[11:]] = dic.get(i[11:],0) + 1
fo = sorted(dic,key=lambda x : dic[x])
for i in fo[:10}:
    print('172.25.254.' + i)
f.close

结果:

猜你喜欢

转载自blog.csdn.net/qq_41911569/article/details/81990029