return statement in functions
hokieghal99
hokiegal99 at hotmail.com
Tue Dec 23 14:05:25 EST 2003
Jp Calderone wrote:
> On Tue, Dec 23, 2003 at 11:44:39AM -0500, hokieghal99 wrote:
>
>>[snip]
>>
>>That works, but it only returns the first item in the list. The
>>functions are returning a list of paths or of files/dirs. For example,
>>the function 'clean_dir_names' should return a complete list of
>>directory names that it has removed bad characters from.
>>
>>def clean_dir_names(path):
>> for root, dirs, files in os.walk(path, topdown=False):
>> for dname in dirs:
>> new_dname = re.sub(bad,'-' ,dname)
>> if new_dname != dname:
>> newpath = os.path.join(root, new_dname)
>> oldpath = os.path.join(root, dname)
>> os.renames(oldpath, newpath)
>> return new_dname
>>
>>How do I make the report function see all of the contents of the list
>>and not just the first item of the list?
>>
>
>
> No. "return new_dname" terminates execution of the function. If you want
> many results, you need to collect them in a list and return the list, or use
> a generator.
>
> Jp
That's odd, it seems silly to terminate something that should be
recursive (os.walk) before it's finished being recursive. What's the
logic behind that idea?
More information about the Python-list
mailing list