3.17作业

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

import os
def file(src_fiule, old_content, new_content):
    with open(r'{}'.format(src_fiule), 'rb') as rf,\
        open(r'{}.swap'.format(src_fiule), 'wb') as wf:
        while True:
            res = rf.readline().decode('utf-8')
            if old_content in res:
                data = res.replace('{}'.format(old_content), '{}'.format(new_content))
                wf.write(bytes('{}'.format(data), encoding='utf-8'))
            else:
                wf.write(bytes('{}'.format(res), encoding='utf-8'))
            if len(res) == 0:
                break
    os.remove(r'{}'.format(src_fiule))
    os.rename('{}.swap'.format(src_fiule), '{}'.format(src_fiule))
    return '修改成功'

if __name__ == '__main__':
    inp_src = input("请输入文件路径:").strip()
    old_content = input("请输入要修改的内容:").strip()
    new_content = input("请输入修改后的内容:").strip()
    if inp_src and old_content and new_content:
        msg = file(inp_src, old_content, new_content)
        print(msg)
    else:
        print("修改失败")

2、编写tail工具

import time
def tail():
with open("log.txt","rb") as f:
f.seek(0,2)
while True:
new_msg = f.readline()
if len(new_msg)==0:
time.sleep(1)
else:
print(new_msg.decode("utf-8"))
tail()

#另一个文件
def write_log():
with open("log.txt","a",encoding="utf-8") as f:
msg = input("请输入日志内容")
f.write(f"\n{msg}")
write_log()

3、编写登录功能

def login(inp_u, inp_p):
    with open(r'db.txt', 'rt', encoding='utf-8') as f:
        for line in f:
            user, pwd = line.strip().split(':')
            if inp_u == user and inp_p == pwd:
                print("登录成功")
                break
        else:
            print("用户名密码错误")
if __name__ == '__main__':
    inp_u = input("用户名:").strip()
    inp_p = input("密码:").strip()
    login(inp_u, inp_p)

4、编写注册功能

def register(inp_u, inp_p):
    with open(r'db.txt', 'a+t', encoding='utf-8') as f:
            f.seek(0, 0)
            for line in f:
                user, *_ = line.strip().split(':')
                if inp_u == user:
                    print("用户名已存在")
                    break
            else:
                f.write('{}:{}\n'.format(inp_u, inp_p))
if __name__ == '__main__':
    inp_u = input("用户名:").strip()
    inp_p = input("密码:").strip()
    register(inp_u, inp_p)

5、编写用户认证功能

def login():
    inp_u = input("用户名:").strip()
    inp_p = input("密码:").strip()
    with open(r'db.txt', 'rt', encoding='utf-8') as f:
        for line in f:
            user, pwd = line.strip().split(':')
            if inp_u == user and inp_p == pwd:
                print("登录成功")
                return True
        else:
            print("用户名密码错误")
            return False
def check_user(user_check):
    if user_check:
        print("有趣的功能")
    else:
        print("请先登录")
def main():
    user_check = False
    msg = """
    1、登录
    2、有趣的功能
    """
    tag = True
    dic = {
        '1': True,
        '2': False
    }
    while tag:
        print(msg)
        num = input("请输入编号:").strip()
        if not num.isdigit() and num not in dic:
            print("必须输入指定编号")
        else:
            if dic[num]:
                user_check = login()
            else:
                check_user(user_check)
if __name__ == '__main__':
    main()
posted @ 2020-03-17 19:34  Python-feng  阅读(138)  评论(0编辑  收藏  举报