'''

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

python实用技巧 : Filtering os.walk(转)的更多相关文章

  1. Python 实用技巧

    模块相关 导入模块时,可以通过模块的 __file__ 属性查看模块所在磁盘的路径位置,参考:关于Python包和模块的10个知识清单 Pip 安装Pip 方法一: sudo apt-get purg ...

  2. python 简单示例说明os.walk和os.path.walk的不同

    import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...

  3. python实用技巧之任务切分

    Python 大任务切分小任务 今天来说说,Python中的任务切分.以爬虫为例,从一个存 url 的 txt 文件中,读取其内容,我们会获取一个 url 列表.我们把这一个 url 列表称为大任务. ...

  4. Python实用技巧

    1.改变工作目录 import os os.chdir('C:/Users/Mr.Zhao') 2.搜索制定目录下的文件 1 import glob 2 glob.glob('C:/User/Mr.Z ...

  5. python 实用技巧:几十行代码将照片转换成素描图、随后打包成可执行文件(源码分享)

    效果展示 原始效果图 素描效果图 相关依赖包 # 超美观的打印库 from pprint import pprint # 图像处理库 from PIL import Image # 科学计算库 imp ...

  6. python中os.walk浏览目录和文件

    #!/usr/bin/env python # 2.py # use UTF-8 # Python 3.3.0 # os.walk()的使用 import os # 枚举dirPath目录下的所有文件 ...

  7. Python os.walk文件遍历用法【转】

    python中os.walk是一个简单易用的文件.目录遍历器,可以帮助我们高效的处理文件.目录方面的事情. 1.载入 要使用os.walk,首先要载入该函数 可以使用以下两种方法 import os ...

  8. Python 之 os.walk()

    原文地址https://www.cnblogs.com/JetpropelledSnake/p/8982495.html          http://www.runoob.com/python/o ...

  9. Python入门之os.walk()方法

    os.walk方法,主要用来遍历一个目录内各个子目录和子文件. os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元 ...

随机推荐

  1. elasticsearch-1.7.1 (es Windows 64)

    elasticsearch-1.7.1 (es Windows 64) https://blog.csdn.net/qq_27093465/article/details/53544541 elast ...

  2. P3457 [POI2007]POW-The Flood

    题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是否是该市的一个组成部 ...

  3. DataBase -- Operator

    TOP子句用于规定要返回的记录的数目. SELECT TOP number | percent column_name FROM table_name LIKE操作符用于在WHERE子句中搜索列的指定 ...

  4. ExtJS 4.1 TabPanel动态加载页面并执行脚本【转】

    ExtJS 4.1 TabPanel动态加载页面并执行脚本 按照官方示例,可以动态加载页面,可是脚本不执行,于是查SDK.google,发现scripts需要设置为true,于是设置该属性,整个代码如 ...

  5. The 13th Zhejiang Provincial Collegiate Programming Contest - C

    Defuse the Bomb Time Limit: 2 Seconds      Memory Limit: 65536 KB The bomb is about to explode! Plea ...

  6. 通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题

    通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题:用nginx来做webserver的时,上传大文件时需要特别注意client_max_body_si ...

  7. Spring学习-- SpEL表达式

    Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpE ...

  8. springmvc4+hibernate4+activiti5.18(Maven)

    项目下载地址: http://files.cnblogs.com/files/walk-the-Line/springmvc_activiti5.18_hibernate4.zip

  9. 转载--博弈问题及SG函数(真的很经典)

    博弈问题若你想仔细学习博弈论,我强烈推荐加利福尼亚大学的Thomas S. Ferguson教授精心撰写并免费提供的这份教材,它使我受益太多.(如果你的英文水平不足以阅读它,我只能说,恐怕你还没到需要 ...

  10. [bzoj3343]教主的魔法——分块

    Brief description 给定一个数列,您需要支持一下两种操作: 给[l,r]同加一个数 询问[l,r]中有多少数字大于或等于v Algorithm analyse 这个题一时想不到什么有效 ...