# coding=utf-8
import socket
import sys
import string
import re
import os
# pattern 匹配模式
# base 搜索路径
# circle 是否递归
def find_file_by_pattern(pattern='.*', base=".", circle=True):
'''''查找给定文件夹下面所有 '''
if base == ".":
base = os.getcwd()
final_file_list = []
cur_list = os.listdir(base)
for item in cur_list:
full_path = os.path.join(base, item)
# 异常处理
if item == ".svn":
continue
if full_path.endswith(".doc") or \
full_path.endswith(".bmp") or \
full_path.endswith(".wpt") or \
full_path.endswith(".dot"):
continue
# 获取文件名
if os.path.isfile(full_path):
if full_path.endswith(pattern):
final_file_list.append(full_path)
else:
if (True == circle):
final_file_list += find_file_by_pattern(pattern, full_path, circle)
return final_file_list
if __name__ == '__main__':
ret = find_file_by_pattern(".txt", ".", True)
print "ret = %s" %(ret)
ret = find_file_by_pattern(".py", ".", False)
print "ret = %s" %(ret)
import socket
import sys
import string
import re
import os
# pattern 匹配模式
# base 搜索路径
# circle 是否递归
def find_file_by_pattern(pattern='.*', base=".", circle=True):
'''''查找给定文件夹下面所有 '''
if base == ".":
base = os.getcwd()
final_file_list = []
cur_list = os.listdir(base)
for item in cur_list:
full_path = os.path.join(base, item)
# 异常处理
if item == ".svn":
continue
if full_path.endswith(".doc") or \
full_path.endswith(".bmp") or \
full_path.endswith(".wpt") or \
full_path.endswith(".dot"):
continue
# 获取文件名
if os.path.isfile(full_path):
if full_path.endswith(pattern):
final_file_list.append(full_path)
else:
if (True == circle):
final_file_list += find_file_by_pattern(pattern, full_path, circle)
return final_file_list
if __name__ == '__main__':
ret = find_file_by_pattern(".txt", ".", True)
print "ret = %s" %(ret)
ret = find_file_by_pattern(".py", ".", False)
print "ret = %s" %(ret)
本文介绍了一个使用Python编写的实用工具,该工具能够根据指定的模式在指定目录及其子目录中查找文件。它排除了特定类型的文件(如.doc、.bmp等),并提供了递归搜索选项。

被折叠的 条评论
为什么被折叠?



