3.17左右 函数注册/登录/用户认证

# 编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def file(filename,old, new):
    import os
    with open(r'{}'.format(filename),mode='rb') as f1, \
            open(r'健康.mp4', mode='wb') as write_f2:
        for line in f1:
            res = line.replace(old, new)
            f1.write(res)
        os.remove(file)
        os.rename('健康.mp4', file)

  编写tail工具

import time
def tail():
    with open("aaa.txt","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 register():
    user_info={}
    with open('register.txt','at',encoding='utf-8')as f:
        for line in f:
            name,psw=line.strip().split(':')
            user_info[name]=psw
    whlie True:
        username=input('请输入您的用户名').strip()
        if username not in user_info:
            print('用户不存在请重新输入')
            continue    
        userpsd=input('请输入密码').strip()
        if userpsd==user_info.get(username):
            print('登录成功')
            break
        else:
            print('登录失败!')

  4、编写注册

def login():
    with open('ttt.txt','a')as f1,\
        open('into.txt','r')as f2:
        inp_name=input('请输入用户名:').strip()
        inp_word=input('请输入密码:').strip()
        for line in f2:
            name,word=line.strip().split(':')
            if inp_name==name:
                return('该用户名已被注册')
        else:
            f1.write('{name}:{psw}\n'.format(name=inp_name,psw=inp_word))
            return ('注册成功')

  

  

  

posted @ 2020-03-17 20:30  清轩挽长风  阅读(129)  评论(0编辑  收藏  举报