zoukankan      html  css  js  c++  java
  • 作业9

    作业9

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

    import os
    
    def swap(path, old_str, new_str):
        """
        swap是修改文件内字符串
        path为修改的文件的路径,
        old_str为要修改的部分
        new_str为修改后的部分
        """
        with open(r"{}".format(path), mode="r", encoding="utf-8") as f1, 
                open(r"{}.swap".format(path), mode="w", encoding="utf-8") as f2:
            for line in f1:
                new_line = line.replace(old_str, new_str)
                f2.write(new_line)
        os.remove(r"{}".format(path))
        os.rename(r"{}.swap".format(path), r"{}".format(path))
    
    swap(r"D:工具pythonpython代码练习day13课程代码.py", "示范", "演示")
    

    2、编写tail工具

    import time
    def tail(path):
        """
        函数用来动态显示新增行内容
        path为文件路径
        """
        with open(path,mode="rb") as f1:
            f1.seek(0,2)
            while 1:
                res = f1.readline()
                if res == b"":
                    time.sleep(1)
                else:
                    print(res.decode("utf-8"))
    
    tail(r"D:工具pythonpython代码练习day13a.txt")
    

    3、编写登录功能,编写用户认证功能

    import os
    # 编写登录功能
    def login():
        inp_user = input("请输入用户名:")
        with open('user_info.txt', mode="r", encoding="utf-8") as f, 
                open("user_info.txt_copy", mode="w", encoding="utf-8") as f1:
            for line in f:
                user, password, count = line.strip().split(":")
                if user == inp_user:
                    if access(inp_user):
                        for i in range(3):
                            inp_password = input("请输入密码:")
                            if password == inp_password:
                                print("登陆成功")
                                while 1:
                                    cmd = input("请输入命令: q退出")
                                    if cmd.upper() == "Q":
                                        break
                                    else:
                                        print("命令{}正在执行".format(cmd))
                                break
                            else:
                                count = int(count)
                                count += 1
                                if count == 3:
                                    print("该账号已锁定")
                                    break
                                count = str(count)
                    else:
                        print("该账号已锁定")
                f1.write("{}:{}:{}
    ".format(user, password, count))
        os.remove("user_info.txt")
        os.rename("user_info.txt_copy", "user_info.txt")
    # 编写用户认证功能
    def access(inp_user):
        with open("user_info.txt", mode="r", encoding="utf-8") as f:
            for line in f:
                user, password, count = line.strip().split(":")
                if inp_user == user:
                    if int(count) >= 3:
                        return 0
                    else:
                        return 1
    
    
    login()
    

    4、编写注册功能

    def logon():
        inp_user = input("请输入用户名:")
        with open("user_info.txt",mode="r",encoding="utf-8") as f:
            for line in f:
                user, password, count = line.strip().split(":")
                if user == inp_user:
                    print("用户名已存在")
                    break
            else:
                inp_password = input("请输入密码:")
                with open("user_info.txt", mode="a", encoding="utf-8") as f1:
                    f1.write("{}:{}:{}
    ".format(inp_user,inp_password,0))
                print("注册成功")
    
    logon()
    
    
    # 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
    # 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
    # 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,
    #    用户B账号加钱
    # 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
    # 4、查询余额功能:输入账号查询余额
    
    https://www.cnblogs.com/achai222/p/12513733.html
    
    
    # 选做题中的选做题:登录功能
    # 用户登录成功后,内存中记录下该状态,
    # 上述功能以当前登录状态为准,必须先登录才能操作
    https://www.cnblogs.com/achai222/p/12513733.html
    
  • 相关阅读:
    数据结构算法练习(一)
    crontab详解
    git遇到问题
    docker容器管理
    docker及服务器遇到的坑
    shell study
    低级终端IO
    高级IO
    信号处理
    UNIX日期与时间
  • 原文地址:https://www.cnblogs.com/achai222/p/12513152.html
Copyright © 2011-2022 走看看