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

时间:2022-08-27 07:43:57

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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高效过滤出文件夹下指定文件名结尾的文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zhangyulin54321/article/details/74180850