首页 > 其他 > 详细

文件操作和序列化

时间:2021-01-21 12:30:01      阅读:41      评论:0      收藏:0      [点我收藏+]

#1.写一个函数如果是py文件就执行,如果是文件夹就执行文件夹下的py文件

#注意 文件路径不能有空格和中文,同一时间执行多台电脑文件
def func(path):
    if os.path.isfile(path) and path.endswith(‘.py‘):
        os.system(‘python %s‘%path)

    elif os.path.isdir(path):
         file_list = os.listdir(path)
         for file in file_list:
             file_new = os.path.join(path,file)
             if os.path.isfile(file_new) and file_new.endswith(‘.py‘):
                 os.system(‘python %s‘%file_new)
func(r‘D:\workspace\com\baizhi\practice\day18‘)

2.写一个copy函数

def copy(path1,path2):
    file = os.path.basename(path1)
    if os.path.isfile(file) and os.path.isdir(path2):
        path2 = os.path.join(path1)
        if os.path.exists(path2):print(‘已有同名文件‘)
        with open(path1,mode=‘rb‘)as f1,                open(path2,mode=‘wb‘)as f2:
                content = f1.read()
                f2.write(content)

3.创建多级目录

os.makedirs(‘glance/cmd‘) #os模块创建文件夹
open(r‘glance\cmd\__init__.py‘,‘w‘).close()#创建文件

4.序列化存储的登录注册

import pickle
def regist():
    user = input(‘输入名字:‘)
    pwd = input(‘输入密码:‘)
    dic ={‘user‘:user,‘pwd‘:pwd}
    with open(‘json_file2‘,mode=‘ab‘) as f:
        pickle.dump(dic,f)
def login():
    user = input(‘输入名字:‘)
    pwd = input(‘输入密码:‘)
    with open(‘json_file2‘,mode=‘rb‘)as f:
        dic = pickle.load(f)
        while dic:
            try:
                if user ==dic[‘user‘] and pwd == dic[‘pwd‘]:
                    print(‘登录成功‘)
                    break
            except EOFError:
                print(‘登录失败‘)
regist()
login()

 

文件操作和序列化

原文:https://www.cnblogs.com/learn-record/p/14307135.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!