How to get the filename in the right case ?

MRAB google at
Fri Sep 26 00:40:56 CEST 2008

On Sep 25, 8:38 pm, Stef Mientki <stef.mien... at> wrote:
> I found a partial workaround that is good enough for me:
> def Get_Windows_Filename ( FileName ) :
>   if == 'nt' :
>     File = os.path.splitext ( FileName .lower ())[0]
>     return glob.glob ( File + '.p?' )
>   return FileName
> This will translate the filename into the correct case, but not the path.
> I also looked at os.path.walk, but that's too slow for me,
> because getting the path correct means I have to start at the root.
You could try win32api.GetLongPathName(...). However, I've found that
some parts of the path are converted to the correct case but other
parts aren't:

>>> print win32api.GetLongPathName(r"c:\documents and settings\administrator\desktop\")
c:\documents and settings\administrator\Desktop\

How does your solution compare to using os.listdir(...) instead of

More information about the Python-list mailing list