今日作业:
一:必做题
1.编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
import os
def file_revise(path,file_msg,new_msg):
with open(path,"r",encoding="utf-8") as f,
open("new.txt","w",encoding="utf-8") as f1:
for line in f:
f1.write(line.replace(file_msg,new_msg))
os.remove("a.txt")
os.replace("new.txt",path)
file_revise("a.txt","hello world!","你好,world!")
2.编写tail工具
import time
def listener(path):
with open(path,'rb') as f:
f.seek(0,2)#将指针移动到文件的最后一行开头
while True:
line=f.readline() #读取一行
if line:
print(line.decode('utf-8'))
else:
#否则等待监听,模拟延时操作
time.sleep(0.2)
listener("access1.log")
3.编写登录功能
def login(name,psd_name):
flag=True
while flag:
with open("name.txt", mode="r", encoding="utf-8") as f:
for line in f:
use_name, password = line.strip().split(":")
if name == use_name and password == psd_name:
print("登录成功!")
flag=False
break
else:
print('登陆失败,退出!')
break
name = input("请输入用户名:").strip()
psd_name = input("请输入密码:").strip()
login(name,psd_name)
4.编写注册功能
def registered(path):
user_name = input('请输入账号:')
usr_psd = input('请输入密码:')
with open(path, 'a', encoding='utf-8')as f:
f.write(f'{user_name}:{usr_psd}
')
registered('a.txt')
二:选做题
编写ATM程序实现下述功能,数据来源于文件db.txt
1.充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2.转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3.提现功能:用户输入提现金额,db.txt中该账号钱数减少
4.查询余额功能:输入账号查询余额
三:选做题中的选做题:登录功能
用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作