作业0317

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

def modify(Absolute_Path,old_contents,new_contents):  # x——修改的文件路径,y——要修改的内容,z——修改后的内容
    '''文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改'''
    with open(Absolute_Path, mode='rt', encoding='utf-8') as f:
        res = f.read()
        data = res.replace(old_contents,new_contents)
        print(data)

    with open(x, mode='wt', encoding='utf-8') as f1:
        f1.write(data)

2、编写tail工具

def tail(Absolute_Path):
    '''编写tail工具,路径为绝对路径'''
    import time
    with open(Absolute_Path, mode='rb') as f:
        f.seek(0, 2)

        while True:
            line = f.readline()
            if len(line) == 0:
                time.sleep(0.3)
            else:
                print(line.decode('utf-8'), end='')

3、编写登录功能

def login(Absolute_Path):
    '''编写登录功能,路径为绝对路径'''

    username = input('请输入您的账号:').strip()
    password = input('请输入您的密码:').strip()

4、编写注册功能

def signin(Absolute_Path):
    '''编写注册功能,路径为绝对路径'''

    username = input('请输入用户名: ')
    password = input('请输入密码: ')
    with open(Absolute_Path, 'w', encoding='utf-8') as f:
        f.write('%s:%s' % (username, password) + '
')

5、编写用户认证功能

def Certification(Absolute_Path):
    '''编写认证功能,路径为绝对路径'''

    username = input('请输入您的账号:').strip()
    password = input('请输入您的密码:').strip()

    with open(Absolute_Path,'r') as f:
        res=f.read()

        user,pwd=res.strip().split(":")
        for x in range(0,3):
            if user == username and pwd == password:
                print('login successful')

            else:
                print('账号或密码错误')
原文地址:https://www.cnblogs.com/zuiyouyingde/p/12515192.html