如何在Windows上使用Python获取长文件系统路径

11

这会返回一个短路径(DOS约定)(在Windows上):

import tempfile
tempDir = tempfile.mkdtemp()
print tempDir

Output >>> c:\users\admini~1\appdata\local\temp\tmpf76unv

注意admini~1

我该如何获取/转换为完整路径?例如C:\ users \ administrator \ appdata ...


@Levon 我尝试了os.path中的许多方法,但都无法解决我的问题。 - Andy Arismendi
1
@AndyArismendi 你为什么需要完整路径? - Ashwini Chaudhary
@AshwiniChaudhary,就我的目的而言,我正在解决第三方模块的故障,并希望将完整路径传递给它,但是没有找到“内置”的方法来获取长路径,尽管我可以想到其他一些需要这样做的原因:1)显示路径,2)当禁用Windows 8.3名称创建时。 - Andy Arismendi
2个回答

10
请尝试以下代码(已更新):

请尝试以下代码(已更新):

from ctypes import create_unicode_buffer, windll
BUFFER_SIZE = 500
buffer = create_unicode_buffer(BUFFER_SIZE)
get_long_path_name = windll.kernel32.GetLongPathNameW
get_long_path_name(unicode(short_path_name), buffer, BUFFER_SIZE)
long_path_name = buffer.value

希望这能有所帮助。请参考http://mail.python.org/pipermail/python-win32/2008-January/006642.html


可以在Python中访问GetLongPathName Win32 API吗? - Sudhir Krishnan

9
tempDir = win32file.GetLongPathName(tempDir)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接