zoukankan      html  css  js  c++  java
  • python实用技巧 : Filtering os.walk(转)


    '''
    Created on Mar 7, 2010

    @author: Diego

    需求: 得到某个目录下, 符合过滤条件的文件夹/文件.
    实现: 将os.walk再次包装.

    TODO: 不知道本程序的做法, 和传统的逐个目录列举的方法, 哪个效率更高. 待测试.

    '''
    import  os
    import  os.path

    os.path.sep
    = " / "
    path 
    =   " /media/dev/project/google_codes/srgjs "
    EXCLUDE_DIR_LIST 
    =  [ " .SVN " , " CVS " ]
    EXCLUDE_FILE_LIST 
    =  [ " .CVSIGNORE " ]


    def  is_parent_exclude(parentPath,excludeDirList):
        ss
    = parentPath.split( " / " );
        
    for  s  in  ss:
            
    if (s.upper()  in  excludeDirList):
                
    return  True
        
        
    return  False

    def  filter_walk(targetDirectory,excludeDirList,excludeFileExtList):
        dirList
    = []
        fileList
    = []
        
    for  (parent, dirs, files)  in  os.walk(targetDirectory):
            
            
    for  d  in  dirs:
                
    if (d.upper()  in  excludeDirList):
                    
    continue
                
                
    # To check if one of the parent dir should be excluded.
                 if (is_parent_exclude(parent,excludeDirList)):
                    
    continue
                
                dirList.append(parent
    + " / " + d)
                
            
            
    for  f  in  files:
                
    if (f.upper()  in  excludeFileExtList):
                    
    continue
                
    # To check if one of the parent dir should be excluded.
                 if (is_parent_exclude(parent,excludeDirList)):
                    
    continue
                
                fileList.append(parent
    + " / " + f)
        
        
    return  (dirList,fileList)            

    # test
    dirs,files  =  filter_walk(path,EXCLUDE_DIR_LIST,EXCLUDE_FILE_LIST)

    for  d  in  dirs:
        
    print  d

    for  f  in  files:
        
    print  f

  • 相关阅读:
    关于s3c6410 实现opengl的分析
    ARM9和ARM11的区别
    ARM9E 和 cortex a8 NEON 优化效率的对比
    armv6 的特点
    AMBA总线新一代标准AXI分析和应用
    linux下如何模拟按键输入和模拟鼠标
    opengl es 学习
    Jlink初使用
    Allegro 制作金手指封装
    世界之窗浏览器设置google搜索[转]
  • 原文地址:https://www.cnblogs.com/mmix2009/p/3211983.html
Copyright © 2011-2022 走看看