'''

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. AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.

    体验最新版AndroidStudio3.0 Canary 8的时候,发现之前项目的butter knife报错,用到注解的应该都会报错 Error:Execution failed for task ...

  2. mac tomcat: error 1 operation not permitted

    在用微信传一个tomcat压缩包到别人后,解压后无法用idea启动,提示 error 1 operation not permitted,但是直接用命令可以执行. 仔细查看,原来是因为mac出于安全考 ...

  3. JSP中的Cookie

    如何创建Cookie 先引包: import="javax.servlet.http.Cookie" JSP是使用如下的语法格式来创建cookie的: Cookie cookie_ ...

  4. Dubbo 项目与传统项目

    1.什么是传统工程 单工程 MVC 架构 控制层通过调用服务层完成业务逻辑处理 业务层调用持久层进程数据操作 2.什么是分布式工程 将传统项目的单工程结构,拆分成多工程 一般会有这几个工程: 父工程: ...

  5. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  6. 12.25模拟赛T2

    https://www.luogu.org/blog/a23333/post-xing-xuan-mu-ni-sai-path-ji-wang-zui-duan-lu 如果设f[i]表示从i到n的期望 ...

  7. Cannot resolve symbol ‘Component’ & Cannot resolve symbol ‘PropTypes’

    import React, { Component, PropTypes } from 'react' 报错:Cannot resolve symbol 'Component' Cannot reso ...

  8. C ------ 标准函数介绍

    sprintf() 函数原型:int sprintf( char *buffer, const char *format [, argument] ... ); 功能介绍: 1.把一个字符串赋值(拷贝 ...

  9. Linux下只允许用户远程scp

    本文将介绍在Linux环境下,让用户不能远程登录 只能使用scp命令 使用到的软件:rssh(http://pizzashack.org/rssh/index.shtml ) 环境:centos6.x ...

  10. x:Class, x:Key

    x:Class: 用来创建一个partial的class, 比如默认生成的x:Class="MyTest.MainWindow", 会自动生成一个MainWindow的partia ...