In Python on a GNU/Linux system, what\'s the fastest way to recursively scan a directory for all .MOV
or .AVI
files, and to store them in a list? <
Python 2.x:
import os
def generic_tree_matching(rootdirname, filterfun):
return [
os.path.join(dirname, filename)
for dirname, dirnames, filenames in os.walk(rootdirname)
for filename in filenames
if filterfun(filename)]
def matching_ext(rootdirname, extensions):
"Case sensitive extension matching"
return generic_tree_matching(
rootdirname,
lambda fn: fn.endswith(extensions))
def matching_ext_ci(rootdirname, extensions):
"Case insensitive extension matching"
try:
extensions= extensions.lower()
except AttributeError: # assume it's a sequence of extensions
extensions= tuple(
extension.lower()
for extension in extensions)
return generic_tree_matching(
rootdirname,
lambda fn: fn.lower().endswith(extensions))
Use either matching_ext
or matching_ext_ci
with arguments the root folder and an extension or a tuple of extensions:
>>> matching_ext(".", (".mov", ".avi"))