zoukankan      html  css  js  c++  java
  • 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" {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('{}:{}
    '.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()
  • 相关阅读:
    Redux API之compose
    Redux API之bindActionCreators
    Django组件-admin
    Django组件-分页器
    Django视图之FBV与CBV
    前端综合练习
    05-前端之jQuery
    关于DOM操作的案例
    04-再探JavaScript
    03-初识JavaScript
  • 原文地址:https://www.cnblogs.com/haliluyafeng/p/12512834.html
Copyright © 2011-2022 走看看