'''

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. JDBC数据源的驱动问题

    classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle  ...

  2. 【bzoj1263】[SCOI2006]整数划分 高精度

    题目描述 从文件中读入一个正整数n(10≤n≤31000).要求将n写成若干个正整数之和,并且使这些正整数的乘积最大. 例如,n=13,则当n表示为4+3+3+3(或2+2+3+3+3)时,乘积=10 ...

  3. HDU 6201 transaction transaction transaction(拆点最长路)

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  4. 2018牛客多校第一场 D.Two Graphs

    题意: n个点,m1条边的图E1,n个点,m2条边的图E2.求图E2有多少子图跟图E1同构. 题解: 用STL的全排列函数next_permutation()枚举映射.对于每一种映射枚举每一条边判断合 ...

  5. 完全解析线程池ThreadPool原理&使用

    目录 1. 简介 2. 工作原理 2.1 核心参数 线程池中有6个核心参数,具体如下 上述6个参数的配置 决定了 线程池的功能,具体设置时机 = 创建 线程池类对象时 传入 ThreadPoolExe ...

  6. CentOS 6通过yum升级Git

    By francis_hao    Mar 9,2017   在一个新机器上推送代码到github上时出现了下面的问题 error: The requested URL returned error: ...

  7. libusb 示例

    #include <usb.h> #include <stdio.h> #define VERSION "0.1.0" #define VENDOR_ID ...

  8. CSS去掉 a 标签点击后出现的虚线框

    方法一: 在a标签里加入js控制,当a标签被聚焦时,强制取消焦点<a href="#" onfocus="this.blur();">测试</ ...

  9. Java并发(7)- 你真的了解ReentrantReadWriteLock吗?

    引言 在前几篇文章中了解了ReentrantLock.Semaphore与CountDownLatch后,J.U.C包中基于AQS实现的并发工具类还剩一个比较重要的:读写锁ReentrantReadW ...

  10. 知问前端——工具提示UI

    工具提示(tooltip),是一个非常实用的UI.它彻底扩展了HTML中的title属性,让提示更加丰富,更加可控制,全面提升了用户体验. 调用tooltip()方法 在调用tooltip()方法之前 ...