作业第十三天文件调用

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def file(path,old,new):
with open(path, mode='rt', encoding='utf-8') as f:
data = f.read()
with open(path1, mode='wt', encoding='utf-8') as f1:
f1.write(data.replace(old,new))
# 2、编写tail工具
import time
def tail():
with open("access.log","rb") as f:
f.seek(0,2)
while True:
line = f.readline()
if len(line) == 0:
time.sleep(0.2)
else:
print(data.decode("utf-8"))
# 3、编写登录功能
def login():
tag = True
while tag:
name = input("请输入用户名:").strip()
with open("user.txt","r",encoding="utf-8") as f:
for line in f:
username, password = line.strip().split(":")
if name == username:
count = 0
while count < 3:
pwd = input("请输入密码:").strip()
if pwd == password:
print("登录成功。")
tag = False
break
else:
print("密码错误")
count +=1
break
else:
print("用户名不存在。")


# 4、编写用户注册功能
def register(username,pwd,identity):
with open("user1.txt", "r", encoding="utf-8") as f:
for line in f:
if username in line:
print("用户{}已被注册".format(username))
break
with open("u_db.txt","a",encoding="utf-8") as fa:
fa.write("{}:{}:{}\n".format(username,pwd,identity))
print("用户{}注册成功。".format(username))
原文地址:https://www.cnblogs.com/lijunc/p/12512811.html