os.walk walks too much

Josef Meile jmeile at hotmail.com
Wed Feb 25 08:21:44 EST 2004


Hi,

> Hello,
> I am using Pyton 2.3
> I desire to walk a directory without recursion
> 
> this only partly works:
> def walk_files() :
>    for root, dirs, files in os.walk(top, topdown=True):
>        for filename in files:
>            print( "file:" + os.path.join(root, filename) )
>        for dirname in dirs:
>             dirs.remove( dirname )
I don't know what this walk function does, but anyway, I
think one problem here is that you are iterating over a
variable that you are changing later. There was a similar
message weeks ago and the solution was to copy the list
and remove the elements of this duplicate. I don't remember
which was the function used to copy the list, but I'm sure
that you can't use:
dirs2=dirs
because they reffer to the same memory address.

Regards,
Josef



More information about the Python-list mailing list