标签:python
# -*- coding:utf-8 -*-
#遍历目录树
import os,fnmatch
def all_files(root, patterns=‘*‘, single_level=False, yield_folder=False):
# 将模式从字符串中取出放入列表中
patterns = patterns.split(‘;‘)
for path, subdirs, files in os.walk(root):
if yield_folder:
files.extend(subdirs)
files.sort()
for fname in files:
for pt in patterns:
if fnmatch.fnmatch(fname, pt):
yield os.path.join(path, fname)
break
if single_level:
break
# fnmatch 来检查文件名匹配模式
# os.path fnmatch os.walk 生成器
thefile=list(all_files(‘E:/projects/test-log4j‘, ‘*.class;*.java;*.properties;*.xml‘))
for item in thefile:
print item
上面选了一个java的idea下的test-log4j应用目录,打印其下面所有的java、class、properties、xml文件全路径。
E:\projects\py>python traverse.py
E:/projects/test-log4j\pom.xml
E:/projects/test-log4j.idea\compiler.xml
E:/projects/test-log4j.idea\misc.xml
E:/projects/test-log4j.idea\modules.xml
E:/projects/test-log4j.idea\workspace.xml
E:/projects/test-log4j.idea\copyright\profiles_settings.xml
E:/projects/test-log4j.idea\libraries\Maven__log4j_log4j_1_2_17.xml
E:/projects/test-log4j.idea\markdown-navigator\profiles_settings.xml
E:/projects/test-log4j\src\main\java\Main.java
E:/projects/test-log4j\src\main\resources\log4j.properties
E:/projects/test-log4j\src\main\resources\log4j.xml
E:/projects/test-log4j\target\classes\Main.class
E:/projects/test-log4j\target\classes\log4j.properties
E:/projects/test-log4j\target\classes\log4j.xml
python基础:匹配指定目录下符合规则的文件,打印文件全路径
标签:python
原文地址:http://blog.51cto.com/coffee/2095033