os.path.walk oddity

Simon Callan simon at callan.demon.co.uk
Thu Feb 15 14:33:31 EST 2001


In message <27Mi6.17200$AH6.2346966 at newsc.telia.net>
          "Fredrik Lundh" <fredrik at pythonware.com> wrote:

> Simon Callan wrote:
> > def toRiscos(arg, dir, names):
> >   temp = names
> 
> this assignment doesn't make a copy, it just adds another reference
> to the "names" array.

<mumble> I throw myself on the mercy of the court, and plead
mitigating circumstances, to wit, having just spent a lot of effort
getting a C++ program to do a deep copy under precisely these
conditions.

> two possible solutions: change the loop to
> 
>     for file in names[:]:

Bingo! This works first time, after allowing for the list changing in
size after a del.

> or get rid of the del statements.  for-in won't loop over an item
> more than once anyway...

The reason for using the del is that os.path.walk() throws a serious
wobbly when it finds that the file has moved away from under it.

Thanks for your help.

Simon

-- 
http://www.callan.demon.co.uk/simon/



More information about the Python-list mailing list