您可以通过链接GetShortPathName
和来做到这一点GetLongPathName
。这在理论上是行不通的,因为您可以通过一些配置设置在 Windows 上禁用短文件名。下面是一些使用 ctypes 的示例代码:
def normcase(path):
import ctypes
GetShortPathName = ctypes.windll.kernel32.GetShortPathNameA
GetLongPathName = ctypes.windll.kernel32.GetLongPathNameA
# First convert path to a short path
short_length = GetShortPathName(path, None, 0)
if short_length == 0:
return path
short_buf = ctypes.create_string_buffer(short_length)
GetShortPathName(path, short_buf, short_length)
# Next convert the short path back to a long path
long_length = GetLongPathName(short_buf, None, 0)
long_buf = ctypes.create_string_buffer(long_length)
GetLongPathName(short_buf, long_buf, long_length)
return long_buf.value