zoukankan      html  css  js  c++  java
  • python使用装饰器对文件进行读写操作'及遍历文件目录

    '''使用装饰器对文件进行读写操作'''
    # def check_permission(func):
    #     '''演示嵌套函数定义及使用'''
    #     def wrapper(*args,**kwargs):
    #         '''*args:接收任意多个实参并存入元组中;**kwargs:接收关键字参数显示赋值并存入字典中'''
    #         if kwargs.get('username') != 'admin':
    #             raise Exception('Sorry,You are not allowed')
    #         return func(*args,**kwargs)
    #     return wrapper
    #
    # class ReadWriteFile(object):
    #     # 装饰器
    #     @check_permission #read = check_permission(read)
    #     def read(self,username,filename):
    #         return open(filename,'r').read()
    #
    #     def write(self,username,filename,content):
    #         with open(filename,'a+') as op:#采用with上下文管理语句
    #             op.write(content)
    #         #open(filename,'a+').write(content)
    #     # 普通函数使用
    #     writes = check_permission(write)
    #
    # t = ReadWriteFile()
    # print(t.read(username='admin',filename=r'c:UsersPGIDYSQDesktop1111111e.gen'))
    # print("*"*60)
    # t.write('admin',filename=r'c:UsersPGIDYSQDesktop1111111e.gen',content='增加内容...')
    # print("-"*60)
    
    # with open(r'c:UsersPGIDYSQDesktop1111111e.gen') as fp:
    #     #print(list(map(len,fp.readlines())))
    #     print(list(enumerate(fp.readlines())))
    #         # for line in fp:
    #         # print(line)
    #pickle使用
    import pickle
    srcurl =r'c:UsersPGIDYSQDesktop1111111e.gen'
    dsturl =r'c:UsersPGIDYSQDesktop	set.bat'
    with open(srcurl,encoding='utf-8') as src,open(dsturl,'wb') as dest:
        lines = src.readlines()
        pickle.dump(len(lines),dest)#行数
        for line in lines:
            pickle.dump(line,dest)
    with open(r'c:UsersPGIDYSQDesktop	set.bat','rb') as fp:
        n = pickle.load(fp)#转换行数号
        for i in range(n):
            #print(pickle.load(fp))
            bb = pickle.load(fp)
            print(bb)
    #struct使用
    #struct.pack,unpack==》write(struct),read(9)
    '''遍历指定目录下的所有子目录及文件'''
    import os
    def visitDir(path):
        if not os.path.isdir(path):
            print('Error')
            return
        for lists in os.listdir(path):
            sub_path = os.path.join(path,lists)
            print(sub_path)
            if os.path.isdir(sub_path):
                visitDir(sub_path)#递归调用
    #visitDir(r'F:UpSVNProject')
    #采用os.walk()方法进行遍历
    def visitDir2(path):
        if not os.path.isdir(path):
            print('Error')
            return
        list_dirs = os.walk(path)
        for root,dirs,files in list_dirs:
            for d in dirs:
                print(os.path.join(root,d))
            for f in files:
                print(os.path.join(root,f))
    #visitDir2(r'F:UpSVNProject')

     

  • 相关阅读:
    常州day2
    常州day3
    常州day1p3
    第3章 图像基础
    第2章 什么是深度学习?
    Deep Learning for Computer Vision with Python 第1章:整个内容简介
    双目视觉(1)---立体匹配介绍
    ubuntu 16.04 配置python远程jupyter nootbook环境
    Ubuntu16.04 Caffe CPU版本 安装步骤记录
    opencv(2)- 处理像素值
  • 原文地址:https://www.cnblogs.com/ysq0908/p/9157395.html
Copyright © 2011-2022 走看看