Created
February 16, 2016 09:59
-
-
Save vladimirgamalyan/b1f1e631435ac9172c39 to your computer and use it in GitHub Desktop.
normalize path letter case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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