函数基本使用的作业

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def file_modify(url,src1,src2):
    with open(url, mode='rt', encoding='utf-8') as f:
        data = f.read()
    with open(url, mode='wt', encoding='utf-8') as f:
        f.write(data.replace(src1, src2))
    return print('修改成功')
file_modify('a.txt','aaa','bbb')

# 2、编写tail工具
import time
def tail(url):
    with open(url,mode='rb') as f:
        f.seek(0,2)
        while True:
            line=f.readline()
            if len(line) == 0:
                time.sleep(0.5)
            else:
                print(line.decode('utf-8'),end='')
tail('access.log')

# 3、编写登录功能
def login(username):
    with open('a.txt', 'r', encoding='utf-8') as f, 
            open('b.txt', 'w', encoding='utf-8') as w:
        for user_pwd in f:
            user, pwd, count = user_pwd.strip().split(':')
            count = int(count)
            if username == user:
                while count < 3:
                    password = input('请输入密码: ').strip()
                    if password == pwd:
                        return print('登录成功!')
                    else:
                        print('登录失败!')
                        count += 1
                if count == 3:
                    print('当前用户以被锁定')
            w.write(f'{user}:{pwd}:{count}
')
    import os
    os.remove('a.txt')
    os.rename('b.txt', 'a.txt')
    return

username = input('输入账号:').strip()
login(username)

# 4、编写注册功能
def register(username):
    with open(r'a.txt', mode='rt', encoding='utf-8') as f, open(r'a.txt', mode='at', encoding='utf-8') as w:
        for line in f:
            name, pwd,count = line.strip().split(':')
            if name == username:
                return print('账号已存在')
        else:
            password = input('输入密码:').strip()
            w.write('{}:{}:0
'.format(username,password))
            return print('注册成功')
username = input('输入账号:').strip()
register(username)
原文地址:https://www.cnblogs.com/bk134/p/12511632.html