Make all files extension lower by a given directory name

Tim Chase python.list at
Thu Nov 2 14:58:20 CET 2006

>> can you post an example of a filename that misbehaves on
>> your machine?
>> </F>
> To Fredrik Lundh and Tim Chase:
> My task was read all tga file names and generate a config xml
> file, then pass this xml file to a utility, but this utillity
> only read files with extension 'tga' than 'TGA'.
> I tried name + ext first but I got two dots between them, so I
> wrote code that way (the strip thing), and it worked. Today it
> does not work (the strip code cut last character from file
> name, like you said :) ), and I changed it back, now
> everything is OK. I really don't know what happened yesterday.

Having a win32 program take case-sensitive filenames is a bit 
odd, given that the OS is case-agnostic...however, that doesn't 
preclude bad programming on the part of your tool-maker.  Alas.

Thus, to accomodate the lousy programming of your tool's maker, I 
proffer this:

I've found that Win32 doesn't often take a rename if the origin 
and destination differ only in case[*].  Thus, to change the 
case, I've had to do *two* to, say, prefix with an 
underscore and change the case to my desired case, and then one 
to strip off the leading underscore.  You might try a similar 
song-and-dance to strong-arm Windows into specifying the case of 
the file.  Something like this (untested) code:

	filename = "Spanish Inquisition.TGA"
	name, ext = splitext(filename)
	intermediate = "_" + name + ext.lower()
	rename(filename, intermediate)
	rename(intermediate, intermediate[1:])

And then I'd wrap this contrived abomination in plenty of 
comments so that folks coming back to it understand why you're 
using three lines to do what "should" be just

	rename(filename, name + ext.lower())

as would be the "right" way to rename the file with a lowercase 
version of its extension.

Yes, it's an odd filename choice there which you might not have 
expected...nobody expects the Spanish Inquisition...


[*] Have had problems with this both in Explorer and at the 
command prompt.

More information about the Python-list mailing list