Tweak normpath_with_actual_case,
@@ -226,19 +226,25 @@ def normpath_with_actual_case(name: str) -> str:
assert os.path.exists(name), "Not exists: " + name
if os.name == "nt":
- name = os.path.realpath(name)
+ # https://stackoverflow.com/questions/2113822/python-getting-filename-case-as-stored-in-windows/2114975
+ name = os.path.normpath(name)
from ctypes import create_unicode_buffer, windll
buf = create_unicode_buffer(512)
+ # GetLongPathNameW alone doesn't fix filename part
windll.kernel32.GetShortPathNameW(name, buf, 512) # @UndefinedVariable
windll.kernel32.GetLongPathNameW(buf.value, buf, 512) # @UndefinedVariable
- if len(buf.value):
result = buf.value
- else:
- result = name
- assert isinstance(result, str)
+ if result.casefold() != name.casefold():
+ # Sometimes GetShortPathNameW + GetLongPathNameW doesn't work
+ # see eg. https://github.com/thonny/thonny/issues/925
+ windll.kernel32.GetLongPathNameW(name, buf, 512) # @UndefinedVariable
+ result = buf.value
+
+ if result.casefold() != name.casefold():
+ result = name
if result[1] == ":":
# ensure drive letter is capital
@@ -246,6 +252,10 @@ def normpath_with_actual_case(name: str) -> str:
else:
return result
else:
+ # easy on Linux
+ # too difficult on mac
+ # https://stackoverflow.com/questions/14515073/in-python-on-osx-with-hfs-how-can-i-get-the-correct-case-of-an-existing-filenam
+ # Hopefully only correct case comes into Thonny (eg. via open dialog)
return os.path.normpath(name)
