return statement in functions

Jp Calderone exarkun at intarweb.us
Tue Dec 23 19:11:58 CET 2003


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


> >
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20031223/8c3fc71c/attachment.pgp>


More information about the Python-list mailing list