py之檔案練習

2021-09-27 06:59:45 字數 1600 閱讀 7087

「」"

生成100個mac位址並寫入檔案中,mac位址前6位(16進製制)為01-af-3b

01-af-3b-xx-xx-xx

-xx01-af-3b-xx

-xx01-af-3b-xx-xx

-xx01-af-3b-xx-xx-xx

「」"

import random

import string

#print(string.hexdigits)

def create_mac():

mac = '01-af-3b'

# 生成16進製制的數

hex_num = string.hexdigits

for i in range(3):

# 從16進製制字串中隨機選擇兩個數字

# 返回值是乙個列表

n = random.sample(hex_num,2)

# 拼接列表的內容 將小寫的字母轉換成大寫的字母

sn = '-' + ''.join(n).upper()

mac += sn

return mac

#主函式 隨機生成100 mac位址

def main():

with open('mac.txt','w') as f:

for i in range(100):

mac = create_mac()

print(mac)

f.write(mac + '\n')

main()

「」"

京東二面程式設計題

#1. 生成乙個大檔案ips.txt,要求1200行,每行隨機為172.25.254.0/24段的ip;

#2. 讀取ips.txt檔案統計這個檔案中ip出現頻率排前10的ip;

「」"

import random

def create_ip_file(filename):

ips = ['172.25.254.' + str(i) for i in range(1,255)]

print(ips)

with open(filename,'a+') as f:

for count in range(1200):

print(random.sample(ips,1))

f.write(random.sample(ips,1)[0] + '\n')

#create_ip_file('ips.txt')

def sorted_ip(filename,count=10):

ips_dict = dict()

with open(filename) as f:

for ip in f:

if ip in ips_dict:

ips_dict[ip] += 1

else:

ips_dict[ip] = 1

sorted_ip = sorted(ips_dict.items(),

key=lambda x:x[1],reverse=true)[:count]

return sorted_ip

print(sorted_ip('ips.txt',20))

py之os模組練習

1.在當前目錄新建目錄img,裡面包含多個檔案,檔名各不相同 x4g5.png 2.將當前img目錄所有以.png結尾的字尾名改為.jpg import random import string import os def gen code len 4 隨機生成4位驗證碼 li random.sam...

py 列表練習

area a北京 q上海 f香港 s澳門 r天津 print area print sorted area print area print sorted area,reverse true print area area.reverse print area area.reverse print ...

py之裝飾器練習(使用者登入)

root admin redhat id id vip 多個裝飾器的應用場景 會採用多個裝飾器先驗證是否登陸成功 再驗證許可權是否足夠 import functools import inspect def is login fun functools.wraps fun if args 0 in ...