'''

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. ASP.NET页面之间传值QueryString(1)

    QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不能用这 ...

  2. POJ——2449 Remmarguts' Date

    Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...

  3. 【题解】[WC2006]水管局长

    感觉这题好强啊……本来以为能过,结果毫无疑问的被ge了一顿……在这里记录一下做的过程,也免得以后又忘记啦. 首先,我们应看出在这张图上,要让经过的水管最长的最短,就是要维护一棵动态的最小生成树.只是删 ...

  4. [Leetcode] remove element 删除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  5. NOIP2010 codevs1069 洛谷P1525 关押罪犯

    Problem Description S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极 不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用 ...

  6. 你对javascript的原生操作或者工具了解多少呢?

    1.有一个长度未知的数组a,如果它的长度为0就把数字1添加到数组里面,否则按照先进先出的队列规则让第一个元素出队. 分析:这道题主要是考核了数组的队列方法和栈方法.另外,原题还有字数限制的,只有在字数 ...

  7. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  8. Install the AWS Command Line Interface on Linux

    Install the AWS Command Line Interface on Linux You can install the AWS Command Line Interface and i ...

  9. Mybatis如何查询部分字段

    解决问题:数据库表里面很多字段不太需要,有时只想取到里面的部分字段的值,如果重新定义 DTO 会比较麻烦. BookMapper.xml 文件中定义如下: ` <!-- Book全部字段 --& ...

  10. 【hdu3080】01背包(容量10^7)

    [题意]n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v).n<=50,v<=10^7 [题解] 先用并查集找出各个联通块. 这题主要就是v ...