[Tutor] Preffered way to search posix filesystem

Kent Johnson kent37 at tds.net
Fri Jan 28 17:23:27 CET 2005


Miles Stevenson wrote:
> What is interesting is that the latest 2.4 Python docs say that walk() returns a Tuple, which is untrue. 
> It returns a generator object according to type(). This had me heavily confused as to how to use 
> what was returned from walk() and it took a good hour of troubleshooting to figure it out.

The docs are correct but maybe a little subtle. They say,

walk(  	top[, topdown=True  [, onerror=None]])
     walk() generates the file names in a directory tree, by walking the tree either top down or 
bottom up. For each directory in the tree rooted at directory top (including top itself), it yields 
a 3-tuple (dirpath, dirnames, filenames).


To someone familiar with Python generators, the words "generates" and "yields" are strong clues that 
walk is a generator and when you iterate over it you get tuples. If you are not familiar with this 
terminology I can see how it would be confusing.

OTOH there are two examples in the same section that show correct usage...

Kent



More information about the Tutor mailing list