• 3.13-文件处理练习


    #一:今日作业:
    #1、编写文件copy工具
    copy_file = input("请输入您需要复制的文件:").strip()
    new_file = input("请输入您需要创建的副本:").strip()
    
    # 此处路径若直接用 copy_file,不进行处理,则Windows默认的路径分隔符""
    # 在python语法中将会被认作转义符,无法正常对文件进行读取
    with open(rf"{copy_file}",mode="rt",encoding="utf-8") as f1,
        open(rf"{new_file}",mode="wt",encoding="utf-8") as f2:              # 在字符串前加r。即非转义的原始字符串
        res = f1.read()                                                     # read功能,从指针当前位置至末尾数据读入内存。将其返回值赋值给res
    # 当使用mode="rt" rt模式打开时,指针一开始在文件开始位置,所以可将文件整段代码读入内存并赋值给res
        f2.write(res)                                                       # 将res中的数据,写入new_file。
    
    
    
    #2、编写登录程序,账号密码来自于文件
    inp_name = input("请输入账号:")
    inp_password = input("请输入密码:")
    
    with open("userdata.txt",mode="rt",encoding="utf-8") as f:
        for line in f:
            username,password = line.strip().split(":")
            if username == inp_name and password == inp_password:
                print("登陆成功!")
                break
        else:
            print("登陆失败!")
    
    
    #3、编写注册程序,账号密码来存入文件
    def register():
        inp_name = input("请输入注册账号:")
    
        with open("userdata.txt",mode="r+t",encoding="utf-8") as f: # 此处必须用r+模式打开,不然指针在末尾,无法遍历
            for line in f:                                          # a模式打开 指针在末尾,无法判断。
                                                                    # 这个line应该就是从当前指针位置开始向下迭代
                username,password = line.strip().split(":")
                if username == inp_name:
                    print("账号已存在,请重新尝试其他用户名!")
                    break
            else:
                    inp_password = input("请设定密码:")
                    f.write(f"{inp_name}:{inp_password}
    ")
    
    #二:周末综合作业:
    # 2.1:编写用户登录接口
    #1、输入账号密码完成验证,验证通过后输出"登录成功"
    #2、可以登录不同的用户
    #3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
    def login():
        import os
        import time
        count = {}
        exist = []
        tag = True
    
        def unlocked():                                                     # 用于解锁
            print("您被锁定,请等待5秒后重试!")
            time.sleep(5)
            os.remove("locked.txt")
            count[inp_name] = 0
    
        while tag:
            inp_name = input("请输入账号(退出输入Q):")
            if inp_name == "q" or inp_name == "Q":
                print("程序已退出。")
                break
            inp_password = input("请输入密码:")
            with open("userdata.txt",mode="rt",encoding="utf-8") as f:
                if os.path.exists('locked.txt'):                              # 查看是否有被锁定账号。
                    with open("locked.txt",mode="rt",encoding="utf-8") as f1: # 对locked.txt中账号进行判断,若是输入的账号,则锁定五秒再解锁。
                        res = f1.read().strip()
                        if res == inp_name:
                            unlocked()
                else:
                    for line in f:                                            # 取出userdata中数据逐行判断
                        username,password = line.strip().split(":")
                        if username == inp_name and password == inp_password:
                            print("登陆成功!")
                            tag = False
                            break
                        elif username == inp_name:                            # 不能单纯判断密码与账号是否一致,还要保存账号用于判断是否存在
                            exist.append(inp_name)
                    else:
                        if inp_name in count and inp_name in exist:           # 必须进行存在性判断,不然会出现对不存在的账号锁定的情况
                            count[inp_name] += 1
                            print(f"登陆失败!尊敬的用户{inp_name},您已输错{count[inp_name]}次,错误3次将被锁定!")
                        elif inp_name in exist:
                            count[inp_name] = 1
                            print(f"登陆失败!尊敬的用户{inp_name},您已输错{count[inp_name]}次,错误3次将被锁定!")
                        else:
                            print("该账号不存在!")
            for i in count:                                                 # 对每个输错的账号都进行计数,当有账号错误计数到3时,创建locked。
                if count[i] == 3:
                    with open("locked.txt",mode="wt",encoding="utf-8") as f:
                        f.write(i)
                    unlocked()
    
    
    
    # 2.2:编写程序实现用户注册后,可以登录,
    #提示:
    while True:
        msg = """
        0 退出
        1 登录
        2 注册
        """
        print(msg)
        cmd = input('请输入命令编号>>: ').strip()
        if not cmd.isdigit():
            print('必须输入命令编号的数字,傻叉')
            continue
    
        if cmd == '0':
            break
        elif cmd == '1':
            login()
            pass
        elif cmd == '2':
            register()
            pass
        else:
            print('输入的命令不存在')
    
        # 思考:上述这个if分支的功能否使用其他更为优美地方式实现
  • 相关阅读:
    Python面向对象高级编程(__slots__、多继承、定制类)-6
    CS231n Lecture6-Training Neural Networks, part I学习笔记
    CS231n Lecture5-Convolutional Neural Networks学习笔记
    CS231n Lecture4-Introduction to Neural Networks学习笔记
    Python面向对象编程(类与实例、数据封装、继承多态、type()、isinstance())-5
    Python模块-4
    alloc和初始化的定义
    块的定义和使用
    属性的定义以及@synthesize的使用
    实例方法和类方法的定义
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12486925.html
Copyright © 2020-2023  润新知