os.walk- все статьи тега
Использование ОС.walk () для рекурсивного обхода каталогов в Python
Я хочу перейти от корневого каталога ко всем другим каталогам внутри и распечатать то же самое. вот мой код: #!/usr/bin/python import os import fnmatch for root, dir, files in os.walk("."): print root print "" for items in fnmatch.filter(files, "*"): print "..." + items print "" и вот мой O / P: . ...Python_Notes ...pypy.py ...pypy.py.save ...classdemo.py ....goutputstream-J9ZUXW ...latest.py ...pack.py ...classdemo.pyc ...Python_Notes~ ... ...
ОС.ходите не копаясь в каталогах ниже
Как мне ограничить os.walk чтобы возвращать только файлы в каталоге, который я предоставляю? def _dir_list(self, dir_name, whitelist): outputList = [] for root, dirs, files in os.walk(dir_name): for f in files: if os.path.splitext(f)[1] in whitelist: outputList.append(os.path.join(root, f)) else: self._email_to_("ignore") return outputList ...