如何将文件夹抓取到索引文件?

Mal*_*u05 0 python

我正在编写一个脚本,它将从某个文件夹树中找到所有*.R3D并将它们编入索引.我已经看到很多关于如何最好地抓取文件夹和子文件夹的例子,但它们看起来并不漂亮,而且因为我在服务器上工作,我希望尽可能小地保持负载.

我的问题:我想找到最有效的方法来抓取文件夹和子文件夹来索引/查找某个文件类型.

phi*_*hag 6

您可能想要使用包装的辅助函数os.walk,如下所示:

import os

def filesByPattern(directory, matchFunc):
  for path,dirs,files in os.walk(directory):
    for f in filter(matchFunc, path):
      yield os.path.join(path, f)

certainFolder = '.'
allR3DFiles = filesByPattern(certainFolder, lambda fn: fn.endswith('.R3D'))
Run Code Online (Sandbox Code Playgroud)