newbie : prune os.walk

Rory Campbell-Lange rory at campbell-lange.net
Thu Mar 10 13:46:16 CET 2005


Hi. How can I list root and only one level down? I've tried setting dirs
= [] if root != start root, but it doesn't work. I clearly don't
understand how the function works. I'd be grateful for some pointers.

Thanks 
Rory

/tmp/test
    |-- 1
    |-- 2
    |-- 3
    |-- 4
    |-- one
    |   |-- 1
    |   |-- 2
    |   |-- 3
    |   |-- 4
    |   `-- subone <- dont want to see this
    `-- two
        |-- 5
        |-- 6
        |-- 7
        `-- 8

3 directories, 12 files

>>> for root, dirs, files in os.walk('/tmp/test', True):
...     print root, dirs, files
... 
/tmp/test ['one', 'two'] ['1', '2', '3', '4']
/tmp/test/one ['subone'] ['1', '2', '3', '4']
/tmp/test/one/subone [] []
/tmp/test/two [] ['5', '6', '7', '8']

-- 
Rory Campbell-Lange 
<rory at campbell-lange.net>
<www.campbell-lange.net>



More information about the Python-list mailing list