Calling a definition

Fredrik Lundh fredrik at
Thu Oct 19 21:22:49 CEST 2006

"elake" (if that's supposed to be swedish, that should be "elak") wrote:

> I have a piece of code that I need some help with. It is supposed (in
> my mind at least) take two arguments, a start path and a file
> extension. Then when called it should return each of the file paths
> that are found matching the criteria. It is only returning the first
> file that it finds. What am I doing wrong?

you can only return from a function once for each call.

> # this is in a file called
> def findFileExt(startPath, fileExt):
>     for root, dirs, files in os.walk(startPath):
>         for file in files:
>             if file.endswith(fileExt):
>                 filePath = str(os.path.join(root, file))
>                 return filePath

and here you're doing exactly that.

if you expect to be able to loop over the return value from findFileExt, 
replace that "return" with a "yield" (this turns the function into a 

if you want to return a sequence, change the loop so it appends stuff to 
a list, and return that list when you're done.


More information about the Python-list mailing list