2012-07-10 76 views
6

长文件系统路径这将返回我的短路径(DOS约定)(在Windows上):如何从蟒蛇获得在Windows

import tempfile 
tempDir = tempfile.mkdtemp() 
print tempDir 

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

通知的admini~1

如何获得/将其转换为完整路径?例如C:\用户\管理员\应用程序数据...

+0

@Levon我尝试了一堆的方法'os.path'但没有什么是做对我来说。 – 2012-07-10 19:47:47

+1

@AndyArismendi为什么你需要完整的路径? – 2012-07-10 19:56:28

+0

@AshwiniChaudhary出于我的目的,我正在排除第三方模块的故障,并且想要在其中引入完整路径,但没有找到“内置”方式来获取漫长的路径,尽管我可能有其他一些原因可能需要这样做: 1)显示路径,2)当[Windows 8.3名称创建被禁用](http://support.microsoft.com/kb/121007)。 – 2012-07-10 21:31:59

回答

9

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

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

+0

可能的答案也可以在http://stackoverflow.com/questions/1587816/is-it-possible-to-access-the-getlongpathname-win32-api-in-python – 2012-07-10 20:14:36

+0

它的窍门。谢谢! – 2012-07-10 21:29:49

4
tempDir = win32file.GetLongPathName(tempDir)