# 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)