Я ищу способ включения/исключения шаблонов файлов и исключения каталогов из вызова os.walk()
.
Вот что я делаю сейчас:
import fnmatch
import os
includes = ['*.doc', '*.odt']
excludes = ['/home/paulo-freitas/Documents']
def _filter(paths):
matches = []
for path in paths:
append = None
for include in includes:
if os.path.isdir(path):
append = True
break
if fnmatch.fnmatch(path, include):
append = True
break
for exclude in excludes:
if os.path.isdir(path) and path == exclude:
append = False
break
if fnmatch.fnmatch(path, exclude):
append = False
break
if append:
matches.append(path)
return matches
for root, dirs, files in os.walk('/home/paulo-freitas'):
dirs[:] = _filter(map(lambda d: os.path.join(root, d), dirs))
files[:] = _filter(map(lambda f: os.path.join(root, f), files))
for filename in files:
filename = os.path.join(root, filename)
print filename
Вопрос: есть ли лучший способ сделать это? Как?