欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-09-11 20:52:12
如下所示: import os def anytrue(predicate, sequence): return true in map(predicat...

如下所示:

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

以上这篇python高效过滤出文件夹下指定文件名结尾的文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。