Make all files extension lower by a given directory name

Tim Chase python.list at tim.thechases.com
Wed Nov 1 15:35:47 CET 2006


>>>             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>
> 
> thanks a lot!
> 
> strip off the last character because if simply add name and
> ext I got result like "FOO..bar", there are two dots. I'm

When you split the filename and extension, the dot/separator 
remains as part of the extension.  If you recombine the two 
pieces, you shouldn't try and add a dot back in.  The code you 
posted originally doesn't do this, but you may have tweaked the 
original code before posting to the list.  If anything, you'd 
want to strip the dot off the left side of the extension rather 
than try and monkey with the right side of the filename portion.

 >>> os.path.splitext('hello.txt')
('hello', '.txt')
 >>> help(os.path.splitext)
Help on function splitext in module ntpath:

splitext(p)
     Split the extension from a pathname.

     Extension is everything from the last dot to the end.
     Return (root, ext), either part may be empty.


-tkc







More information about the Python-list mailing list