_win.py 851 B

1234567891011121314151617181920212223
  1. from __future__ import annotations
  2. def get_short_path_name(long_name: str) -> str:
  3. """Gets the short path name of a given long path - http://stackoverflow.com/a/23598461/200291."""
  4. import ctypes # noqa: PLC0415
  5. from ctypes import wintypes # noqa: PLC0415
  6. GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW # noqa: N806 # ty: ignore[unresolved-attribute]
  7. GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
  8. GetShortPathNameW.restype = wintypes.DWORD
  9. output_buf_size = 0
  10. while True:
  11. output_buf = ctypes.create_unicode_buffer(output_buf_size)
  12. needed = GetShortPathNameW(long_name, output_buf, output_buf_size)
  13. if output_buf_size >= needed:
  14. return output_buf.value
  15. output_buf_size = needed
  16. __all__ = [
  17. "get_short_path_name",
  18. ]