1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def amend(new,chu,zhang):
import os
with open('a.txt',mode='rt',encoding='utf-8')as f_read,\
open('a.txt.swap',mode='wt',encoding='utf-8')as f_write:
for line in f_read:
if 'b.txt' in line:
line = line.replace('chu','zhang')
f_write.write(line)
os.remove('a.txt')
os.rename('a.txt.swap','a.txt')
amend(r'E:/untitled/day07/a.txt','alex','SB')
2、编写tail工具
import time
with open(r"/untitled/day06/access.log", mode="rb") as f:
f.seek(0, 2) # 快速将指针移动到文件末尾
while True:
line = f.readline()
if len(line) == 0:
time.sleep(0.1)
else:
print(line.decode('utf-8'),end='')
3、编写登录功能
def login():
count = 1
while count < 3:
with open('a.txt', mode='rt', encoding='utf-8')as f:
for line in f:
name, pwd = line.strip('\n').split('😂
if inp_name == name and inp_pwd == pwd:
print('login successful')
count = 6
break
else:
print('user or password error')
count + 1
login()
4、编写注册功能
def register():
with open('a.txt',mode='at',encoding='utf-8')as f1:
f1.write('%s:%s\n'%(user_name,user_pwd))
register()
5、编写用户认证功能
def auth():
with open('a.txt',mode='rt',encoding='utf-8')as f:
for line in f:
name, pwd = line.strip('\n').split('😂
if user_name == name and user_pwd == pwd:
return '认证成功'
res=auth()
print(res)
选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
4、查询余额功能:输入账号查询余额
选做题中的选做题:登录功能
用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作