• 环境小硕的转行之路-22-os,pickle的作业


    pickle和os的作业

    #1.写一个函数,接受一个参数,如果是文件就执行这个文件,如果是文件夹,就执行这个文件夹下面所有的文件.
    import os
    import json,pickle
    import random
    def judge_py_class(name):#判断是否是可执行文件
        if os.path.basename(name).endswith('.py'):
            return True
        else:
            return False
    def implement(path):#执行函数
        if os.path.isdir(path):#判断路径是否是文档
            the_former_address = os.path.split(path)[0]#取前面的绝对地址
            for file in path:
                if not judge_py_class(file):
                    pass
                else:
                    abs_path = os.path.join(the_former_address,file)
                    os.system('python %s'%abs_path)#执行文件
        elif os.path.isfile(path):#判断是否是文件
            if not judge_py_class(path):
                pass
            else:
                os.system('python %s'%path)
    # implement(r'C:UsersAdministratorPycharmProjects全栈学习day191os模块.py')
    #2.写一个copy函数,接受两个参数,第一个是参数是原文件的位置,第二个参数是目标文件的位置
    def copy_func(original_path,copy_path):
        lst = []
        if os.path.isdir(copy_path):
            file_name = os.path.basename(original_path)#取后面的文件名
            path2 = os.path.join(copy_path,file_name)
            if os.path.exists(path2):#判断路径下是否有同名文件
                print('已经有同名文件')
            elif os.path.isfile(path2):#判断路径是否是个文件
                with open('%s'%original_path,'read') as fr:
                    for line in fr:
                        lst.append(line)
                with open('%s'%os.path.join(original_path,file_name),'w') as fw:
                    for line in lst:
                        fw.write(line)
        else:
            print('invalid path')
    
    #3.获取某个文件的上级目录 
    os.path.split(path)[1]
    #4.写一个用户注册登陆程序,每一个用户注册都要把用户名和密码以字典形式写入文件,在登陆的时候再进行验证.
    
    #思路1:直接用一个字典存储所有的信息,内存应该是不够的
    def the_register_func(username,password):#注册函数
        with open('database.dump','rb') as fr:
            temporary_dic = pickle.loads(fr.read())#loads将type转换成dict.
        if username in temporary_dic.keys():#判断是否存在已知用户名
            print('the username now exists')
        else:
            temporary_dic[username] = password#注册账户
        with open('database.dump','wb') as fw:
            fw.write(pickle.dumps(temporary_dic))
    
            def log_func(username, password):
                with open('database.dump', 'rb') as fr:
                    temporary_dic = pickle.loads(fr)
                    if username not in temporary_dic.keys():
                        print('the username do not exist.')
                    elif temporary_dic[username] != password:
                        print('wrong password')
                    else:
    
    #思路2:分别用单个字典存储信息,循环取出
    # {'usr':'alex','pwd':'alex3714'}
     import pickle
     def register():
         usr = input('username :')
         pwd = input('password : ')
         dic = {usr:pwd}
         with open('userinfo','ab') as f:
             pickle.dump(dic,f)
         print('注册成功')
    
     def login():
         flag = True
         usr = input('username :')
         pwd = input('password : ')
         with open('userinfo', 'rb') as f:
             while flag:
                 try :#这样可能做成迭代器会省内存点
                     dic = pickle.load(f)
                     for k,v in dic.items():
                         if k == usr and pwd == v:
                             print('登录成功')
                             flag = False
                             break
                 except EOFError:
                     print('登录失败')
                     break
    

      

  • 相关阅读:
    visual sudio开发工具使用小技巧
    JS去除右边的逗号
    下拉标题
    sp_addextendedproperty
    触发器的使用
    缺失一个正数
    组合总和 去重
    拖动 Drag
    n皇后问题
    括号生成
  • 原文地址:https://www.cnblogs.com/negu/p/11733102.html
Copyright © 2020-2023  润新知