import os import os.path # 获取文件内容 def get_file(): file_path = ‘C:/Users/win10/Desktop/user.txt‘ file_name = open(file_path, "r+") i = int(0) lists = [] # 保存替换过的 for line in file_name.readlines(): i = i+1 # 替换换行符 再切割成数组 lists.insert(i,line.split(‘,‘)) file_name.close() #关闭文件 return lists # 重写内容 def write_file(write_list): file_path = ‘C:/Users/win10/Desktop/user.txt‘ file_write = open(file_path,"w") file_write.write(write_list) file_write.close() # 关闭文件 return lnet = 0 #下一个数 times = 0 sum = len(get_file()) while lnet < sum: k = 0 # 用于计算第几个循环 lists = get_file() content = ‘‘ username = input(‘username:‘) password = input(‘password:‘) status = 0 #状态标记 for item in lists: num = int(item[2].replace(‘\n‘,‘‘)) # break if num >=3 and username == item[0]: print(‘该用户已被锁定‘) lnet = sum #退出while status = 1 break elif item[0] == username and item[1] == password: print(‘欢迎使用‘) lists[k][2] = lists[k][2].replace(‘1‘,‘0‘).replace(‘2‘,‘0‘).replace(‘3‘,‘0‘) for it in lists: content += ‘,‘.join(it) write_file(content) status = 1 lnet = sum # 退出while break elif item[0] == username and item[1] != password: print(‘账号或密码错误‘) lists[k][2] = lists[k][2].replace(str(num),str(num+1)) for it in lists: content += ‘,‘.join(it) write_file(content) status = 1 break # elif item[0] != username: # print(‘账号或密码错误‘) if times == 2: print(‘您已输如超过三次错误!请关闭后重新打开‘) elif status == 0: print(‘账号或密码错误‘) times = times + 1 lnet += 1
文本格式: 账号 密码 次数 admin,admin,0 adminasdad,adadsad,0 adasda,adasdad,1 注:次数是从零开始计算的 用户名不能重复
时间: 2024-11-07 01:16:42