1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
2、编写tail工具
def tail(x):
with open(x, mode='rt', encoding='utf-8') as f:
for i in f:
print('原文件:', i)
print('新文件:')
while True:
res = f.readline()
if len(res) == 0:
continue
else:
print(res)
tail('x.txt')
3、编写注册登录功能
def user_register():
print('注 册')
user = input('请输入帐号: ')
password = input('请输入密码:')
with open(r'userlist.txt', mode='at', encoding='utf-8') as f:
f.write(f'{user}:{password:}
')
user_register()
def login():
print('登 录')
userinfo = input('请输入帐号: ')
pwd= input('请输入密码:')
with open(r'userlist.txt', 'r', encoding='utf-8') as f:
for line in f:
username, password = line.strip().split(":")
if username == userinfo and pwd == password:
print("登录成功")
break
else:
print("输入错误")
login()