如何在Windows上从python获取长文件系统路径

问题描述:

这为我返回了一条短路径(DOS约定)(在Windows上):

This returns me a short path (DOS convention) (on Windows):

import tempfile
tempDir = tempfile.mkdtemp()
print tempDir

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

注意admini~1.

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

How can I get/convert this to a full path? e.g. C:\users\administrator\appdata...

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

Please try the following code (updated):

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

Hope this helps. Please refer to http://mail.python.org/pipermail/python-win32/2008-January/006642.html