python高效过滤出文件夹下指定文件名结尾的文件实例

如下所示:

import os

def anyTrue(predicate, sequence):
return True in map(predicate, sequence)

def filterFiles(folder, exts):
for fileName in os.listdir(folder):
if os.path.isdir(folder + '/' + fileName):
   filterFiles(folder + '/' + fileName, exts)
elif anyTrue(fileName.endswith, exts):
print fileName

exts = ['.md', '.yml', '.rst']
filterFiles('D:\\Twisted\\twisted-trunk', exts)


import os
import time
from itertools import imap
def anyTrue(predicate, sequence):
 return True in imap
(predicate, sequence)
 
def filterFiles(folder, exts):
 for fileName in os.listdir(folder):
  if os.path.isdir(folder + '/' + fileName):
   filterFiles(folder + '/' + fileName, exts)
  elif anyTrue(fileName.endswith, exts):
   print fileName

start = time.time()
exts = ['.md', '.yml', '.rst']
filterFiles('D:\\Twisted\\twisted-trunk', exts)
print('total time=%f' %(time.time()- start))

相关推荐