day13作业

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改

def file_modify(file_path,change_data,data):
    """对文件内容进行修改"""
    with open(file_path,"r",encoding="utf-8") as f:
        info = f.read()
    info = info.replace(change_data,data)
    with open(file_path,"w",encoding="utf-8") as f:
        f.write(info)

# 2、编写tail工具

import time

def tail_tool():
    with open("access.log","rb") as f:
        f.seek(0,2)
        while True:
            data = f.readline()
            if len(data) == 0:
                time.sleep(0.5)
            else:
                print(data.decode("utf-8"))


# 3、编写登录功能

def log_in():
    tag = True
    while tag:
        name = input("Name:").strip()
        with open("info","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("Password:").strip()
                        if pwd == password:
                            print("登录成功。")
                            tag = False
                            break
                        else:
                            print("密码错误")
                            count +=1
                    break
            else:
                print("用户名不存在。")


# 4、编写注册功能

def register():
    name = input("请输入注册的用户名:").strip()
    pwd = input("请输入用户名密码:").strip()
    with open("info","a",encoding="utf-8") as f:
        info_str = "{}:{}\n".format(name,pwd)
        f.write(info_str)
    print("注册成功。")

猜你喜欢

转载自www.cnblogs.com/liqianxin/p/12510951.html