Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse of os.path.normcase on Windows

Is there a simple way to get the "real" case sensitive path from a all lower case path. Like the reverse of os.path.normcase.

For example, consider the directory:

c:\StackOverFlow

If I have the following snippet, how to obtain d_real?

>>> import os
>>> d = os.path.normcase('C:\\StackOverFlow') # convert to lower case
>>> d
'c:\\stackoverflow'
>>> d_real = ... # should give 'C:\StackOverFlow' with the correct case
like image 221
pkit Avatar asked Dec 04 '09 11:12

pkit


1 Answers

I wouldn't consider this solution simple, but what you can to is:

import os
d = os.path.normcase('C:\\StackOverFlow')
files = os.listdir(os.path.dirname(d))
for f in files:
  if not d.endswith(f.lower()):
    continue
  else
    real_d = os.path.join(os.path.dirname(d), f)

It's probably not efficient (depending on the number of files in the directory). It needs tweaking for the path-components (my solution really only corrects the case of the file name and doesn't care about the directory names). Also, maybe os.walk could be helpful to traverse down the tree.

like image 135
jhwist Avatar answered Oct 07 '22 15:10

jhwist