# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def file_change(path,file,c_file):
with open(path,mode='rb')as f:
x=f.read().replace(file,c_file)
with open(path,mode='wb')as f1:
f1.write(x)
# 2、编写tail工具
def tail(path):
import time
with open(path,mode='rb')as f:
f.seek(0,2)
while 1:
x=f.readline()
if len(x)==0:
time.sleep(0.6)
else:
print(x.decode('utf-8'),end='')
# 3、编写登录功能
def login(dic,tag):
while tag:
inp_name = input('请输入您的用户名:')
inp_pwd = input('请输入您的密码:')
if inp_name in dic:
if dic.get(inp_name)[1] >= 3:
print('该账户已锁定')
tag = False
elif inp_pwd == dic.get(inp_name)[0]:
print('登录成功')
tag = False
elif inp_pwd != dic.get(inp_name)[0]:
print('账号或密码 错误')
dic.get(inp_name)[1] += 1
print(f'用户名{inp_name} 已错误{dic.get(inp_name)[1]}次')
dic1 = str(dic)
z = dic1.strip("{}'[]").replace("': ['", ":").replace("', ", ":").replace("], '", "\n")
with open(r'D:\cool\user', mode='wt', encoding='utf-8') as a:
a.write(z)
if dic.get(inp_name)[1] == 3:
print('锁定')
tag = False
else:
print('用户名不存在\n返回 请按[2]\n退出 请按[0]')
cmd2 = input('请输入命令编号>>:')
if cmd2 == '2':
continue
elif cmd2 == '0':
print('退出')
tag = False
# 4、编写注册功能
def registered(tag):
while tag:
dic2 = {}
with open(r'D:\cool\user',mode='rt',encoding='utf-8') as i:
# print(x.read())
for o in i:
name,password,count=o.strip().split(':')
dic2[name]=[password,count]
dic2.get(name)[1] = int(dic2.get(name)[1])
inp_user = input('请输入您想注册的用户名:')
inp_password = input('请输入你的注册密码:')
inp_password2 = input('请再次输入你的注册密码:')
print(dic2)
if inp_user in dic2:
print('该用户名已被注册')
break
else:
if inp_password == inp_password2:
with open(r'D:\cool\user', mode='at', encoding='utf-8') as z:
z.write(f'\n{inp_user}:{inp_password}:0')
print('注册成功')
break
else:
print('注册密码有误\n请重新输入')