more os.walk() issues... probably user error

Kent Johnson kent37 at tds.net
Wed Feb 16 22:04:55 EST 2005


rbt wrote:
>>>> ##      for fs in fs_objects:
>>>> ##
>>>> ##            for f in fs[2]:
>>>> ##                if f in file_skip_list:
>>>> ##                    print f
>>>> ##                    fs[2].remove(f)
>>>> ##
>>>> ##            for d in fs[1]:
>>>> ##                if d in dir_skip_list:
>>>> ##                    print d
>>>> ##                    fs[1].remove(d)
> 
> Will the changes I made (file and dir removals from os.walk()) be 
> reflected in the generator object? Is it safe to remove objects this way 
> and pass the results in a generator on to another function? Sorry for 
> all the questions, I just like to fully understand something before I 
> start doing it with confidence.

Yes. The docs for os.walk() explicitly state, "When topdown is true, the caller can modify the 
dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into 
the subdirectories whose names remain in dirnames."

So changes to the dir list affect the iteration; changes to the file list directly affect the value 
you return to the caller.

Kent



More information about the Python-list mailing list