How to get long file system path from python on Windows

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 22:30:23

问题


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

Notice the admini~1.

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


回答1:


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

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




回答2:


tempDir = win32file.GetLongPathName(tempDir)


来源:https://stackoverflow.com/questions/11420689/how-to-get-long-file-system-path-from-python-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!