Code: Alles auswählen
import os
def find(path='.', condition=None):
if not condition:
condition = lambda _: True
for root, dirs, files in os.walk(path):
for dirname in dirs:
pathname = os.path.join(root, dirname)
if condition(pathname):
yield pathname
for filename in files:
pathname = os.path.join(root, filename)
if condition(pathname):
yield pathname
Code: Alles auswählen
from fnmatch import fnmatch
import os
from os.path import expanduser, isdir
# alle Verzeichnisse im Persönlichen Ordner
for dirpath in find(expanduser('~'), isdir):
print dirpath
# alle *.py-Dateien ausgehend vom Wurzelverzeichnis
for path in find(os.sep, lambda p: fnmatch(p, '*.py')):
print path