[Tutor] Problem with os.walk

Karl Pflästerer sigurd at 12move.de
Tue May 11 08:28:38 EDT 2004


On 11 May 2004, Isr Gish <- isrgish at fastem.com wrote:

> The docs say that you can del the dirnames list. But when I tried doing it, it
> didn't work. Here is the help info:

[...]

> I tried del and dirnames = dirnames[0:0]
> Both didn't work. The only thing that wOrked was.
> for d in dirnames[:]:
>     dirnames.remove(d)


What did you write exactly?  I tried the example from the docs and it
worked as expected

import os
from os.path import join, getsize
for root, dirs, files in os.walk('.'):
    print root, "consumes",
    print sum([getsize(join(root, name)) for name in files]),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

I ran that in a directory where if have a CVS subdirectory and that
didn't get visited.



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list