2023年6月23日发(作者:)
4道Python⽂件操作和函数练习题⼀.利⽤b模式,编写⼀个cp⼯具,要求如下:1. 既可以拷贝⽂本⼜可以拷贝视频,图⽚等⽂件2. ⽤户⼀旦参数错误,打印命令的正确使⽤⽅法,如usage:
cp source_file target_file提⽰:可以⽤import sys,然后⽤获取脚本后⾯跟的参数#Python学习交流群:531509025# cp⼯具import sysif len() != 3: print("usage: cp source_file target_file") ()else: source_file, target_file = [1], [2] with open(source_file,"rb") as read_f,open(target_file,"wb") as write_f: for line in read_f: write_(line)⼆.Python实现 tail -f 功能#tail -f⼯具import sys,timeif len() != 2: print("usage: tail file_name") ()else: file_name = [1] with open(file_name,'rb') as f: (0,2) # 每次都从⽂件末尾开始读 while True: line = ne() if line: print(('utf-8'),end='') # 读取的每⼀⾏都去掉⾏尾的换⾏符 (1)有待优化,每次打开应该显⽰最后10⾏。三.⽂件的修改⽂件的数据是存放于硬盘上的,因⽽只存在覆盖、不存在修改这么⼀说,我们平时看到的修改⽂件,都是模拟出来的效果,具体的说有两种实现⽅式:⽅式⼀:将硬盘存放的该⽂件的内容全部加载到内存,在内存中是可以修改的,修改完毕后,再由内存覆盖到硬盘(word,vim,nodpad++等编辑器)import os
with open('') as read_f,open('.','w') as write_f: data=read_() #全部读⼊内存,如果⽂件很⼤,会很卡 data=e('alex','SB') #在内存中完成修改
write_(data) #⼀次性写⼊新⽂件
('')('.','')⽅式⼆:将硬盘存放的该⽂件的内容⼀⾏⼀⾏地读⼊内存,修改完毕就写⼊新⽂件,最后⽤新⽂件覆盖源⽂件import os
with open('') as read_f,open('.','w') as write_f: for line in read_f: line=e('alex','SB') write_(line)
('')('.','')
三.全局替换程序:写⼀个脚本,允许⽤户按以下⽅式执⾏时,即可以对指定⽂件内容进⾏全局替换替换完毕后打印替换了多少处内容#Python学习交流群:531509025import sysimport os
if len() != 4: print("usage: python3 replace old_str new_str filename") ()else: old_str = [1] new_str = [2] filename = [3] filename_swap = [3] + ".swap" with open(filename,"r",encoding="utf-8") as read_f,open(filename_swap,"w",encoding="utf-8") as write_f: count = 0 for line in read_f: line = e(old_str,new_str) write_(line) num = (new_str) count += 1 totle = count * num print("⼀共替换了%s处内容" % totle) (filename) (filename_swap,filename)四.模拟登陆:⽤户输⼊帐号密码进⾏登陆⽤户信息保存在⽂件内⽤户密码输⼊错误三次后锁定⽤户,下次再登录,检测到是这个⽤户也登录不了user_:123:1alex:456:1jay:789:1import getpassimport os
user_dict = {}with open("user_", "r", encoding="utf-8") as user_list_flie: for line in user_list_nes(): user_list = ().split(":") # print(user_list) _user = user_list[0].strip() _pwd = user_list[1].strip() _lockaccount = int(user_list[2].strip()) user_dict[_user] = {"user": _user, "pwd": _pwd, "lockaccount": _lockaccount} # print(user_dict[_username]) # print(user_dict)
exit_flag = Falsecount = 0while count < 3 and not exit_flag: user = input('n请输⼊⽤户名:') if user not in user_dict: count += 1 print("n⽤户名错误") elif user_dict[user]["lockaccount"] > 0: print("n⽤户已被锁定,请联系管理员解锁后重新尝试") break else: while count < 3 and not exit_flag: pwd = s('n请输⼊密码:') # pwd = input('n请输⼊密码:') if pwd == user_dict[user]["pwd"]: print('n欢迎登陆') print('..........') exit_flag = True else: count += 1 print('n密码错误') continue if count >= 3: # 尝试次数⼤于等于3时锁定⽤户 if user == "": print("n您输⼊的错误次数过多,且⽤户为空") elif user not in user_dict: print("n您输⼊的错误次数过多,且⽤户 %s 不存在" % user) else: user_dict[user]["lockaccount"] += 1 # print(user_dict[user]["lockaccount"]) with open("user_", "r", encoding="utf-8") as user_list_file, open("use_", "w",encoding="utf-8") as new_user_list_file: for new_line in user_dict: new_user_list = [str(user_dict[new_line]["user"]), str(user_dict[new_line]["pwd"]), str(user_dict[new_line]["lockaccount"])] # print(new_user_list) user_str = ":".join(new_user_list) print(user_str) new_user_list_(user_str + "n") ("user_") ("use_", "user_") print("n您输⼊的错误次数过多,%s 已经被锁定" % user)
发布者:admin,转转请注明出处:http://www.yc00.com/web/1687516574a16271.html
评论列表(0条)