Make all files extension lower by a given directory name

Fredrik Lundh fredrik at pythonware.com
Wed Nov 1 11:58:02 CET 2006


Tiefeng Wu wrote:

> I need make all files extension to lower by a given directory.
> Here is my solution:
> 
> import os
> 
> def make_all_file_ext_lower(root):
>     for path, subdirs, files in os.walk(root):
>         for file in files:
>             (name, ext) = os.path.splitext(file)

the parens around the target variables are not necessary.

>             fullname = name[:len(name) - 1] + ext.lower()

are you sure you want to strip off the last character in the actual 
filename?  should "FOO.BAR" really be turned into "FO.bar" ?

name[:len(name) - 1] can be written name[:-1], btw.

>             os.rename(os.path.join(path, file), os.path.join(path, fullname))
> 
> I know this code is lame

except for the potential bug, and possibly the lack of error handling 
for the os.rename call, I'm not sure why you think that.

</F>




More information about the Python-list mailing list