Обход директорий

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import os

def dir_walker(root, filter_fn=lambda f: True):
    if root.startswith('.'):
        root = os.path.abspath(root)
    root = os.path.expanduser(os.path.expandvars(root))

    if filter_fn(root):
        for fname in os.listdir(root):
            pth = os.path.join(root, fname)
            if os.path.isdir(pth):
                for entry in dir_walker(pth):
                    yield entry
            yield pth

readable = lambda f: os.access(f, os.R_OK)
writeable = lambda f: os.access(f, os.W_OK)

for f in dir_walker('.', readable):
    print f
25 марта 2012, 15:14 1 s0rg
blog comments powered by Disqus