在Python中可以访问GetLongPathName() Win32 API吗?

4 投票
2 回答
3056 浏览
提问于 2025-04-15 15:10

我需要把8.3格式的路径转换成完整路径。在Perl中,我可以使用Win32::GetLongPathName(),正如在如何用Perl从8.3 DOS路径获取完整的Win32路径?中提到的那样。但是,我需要在Python中做到这一点。

2 个回答

5

使用来自 win32fileGetLongPathName 函数

import win32file
print win32file.GetLongPathName(r'C:\progra~1')

输出结果:

C:\Program Files
8

使用 ctypes 这个库,它是Python自带的,不需要用到 pywin32 API。可以这样做:

from ctypes import *

buf = create_unicode_buffer(260)
GetLongPathName = windll.kernel32.GetLongPathNameW
rv = GetLongPathName(path, buf, 260)
print buf.value

来自 http://mail.python.org/pipermail/python-win32/2008-January/006642.html

撰写回答