Skip to content

Instantly share code, notes, and snippets.

@vladimirgamalyan
Created February 16, 2016 09:59
Show Gist options
  • Save vladimirgamalyan/b1f1e631435ac9172c39 to your computer and use it in GitHub Desktop.
Save vladimirgamalyan/b1f1e631435ac9172c39 to your computer and use it in GitHub Desktop.
normalize path letter case
"""
normalize_path_case convert path
c:\foo\bar.txt
to
C:\Foo\Bar.txt
(as the file presents at file system, include case resgister)
"""
def correct_case(where, name):
if where.endswith(':'):
where += '/'
l = filter(lambda x: x.lower() == name.lower(), os.listdir(where))
if (len(l) == 1):
return l[0]
return ''
def normalize_path_case(path):
parts = os.path.abspath(path).split(os.sep)
result = parts.pop(0).upper()
for p in parts:
c = correct_case(result, p)
if not c:
return ''
result += '/' + c
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment