[Tutor] Problem with os.walk
Isr Gish
isrgish at fastem.com
Tue May 11 21:30:11 EDT 2004
Thanks Timyfor the help.
I hadn't relized that it has to be changed *in place*.
Isr
-----Original Message-----
>From: "Tim Peters"<tim.one at comcast.net>
>Sent: 5/11/04 10:56:00 AM
>To: "Isr Gish"<isrgish at fastem.com>
>Cc: "tutor at python.org"<tutor at python.org>
>Subject: RE: [Tutor] Problem with os.walk
>
>[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
>assignment)"
>
>"in-place" means things of the form
>
> del dirnames[1:]
>
>or
>
> 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
>are
>
> del dirnames[:]
>
>or
>
> 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