[Tutor] Problem with os.walk

Tim Peters tim.one at comcast.net
Tue May 11 10:56:00 EDT 2004

[Isr Gish]
> The docs say that you can del the dirnames list.

"the caller can modify the dirnames list in-place (e.g., via del or slice

"in-place" means things of the form

    del dirnames[1:]


    dirnames[:] = ['new directory']

> But when I tried doing it, it didn't work.

You have to show the exact code you used, else nobody can guess.


> I tried del

That's not enough to guess.  Maybe you did

    del dirnames

?  That indeed would not work.

> and dirnames = dirnames[0:0]

The binds dirnames to a brand new object, so is not an in-place modification
either.  If you want to empty dirnames entirely, two ways to do it in-place

    del dirnames[:]


    dirnames[:] = []

> Both didn't work. The only thing that wOrked was.
> for d in dirnames[:]:
>     dirnames.remove(d)

The two above will also work.

> And if I didn't make a copy it didn't work correctly. (it seems
> that for i in list works with indexing behindethe seigns).

Yes.  This is documented in the Language Reference manual (not the Library
Reference manual), in the section on "for" loops.

More information about the Tutor mailing list