Andy Arismendi
Andy Arismendi

Reputation: 52699

How to get long file system path from python on 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

Notice the admini~1.

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

Upvotes: 12

Views: 4319

Answers (3)

Kelvin Jimenez
Kelvin Jimenez

Reputation: 11

A solution without invoking Windows specific APIs will be using pathlib.

from pathlib import Path  
Path(tempDir).resolve()

Upvotes: 1

Sudhir Krishnan
Sudhir Krishnan

Reputation: 281

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

Upvotes: 10

Andrew
Andrew

Reputation: 1618

tempDir = win32file.GetLongPathName(tempDir)

Upvotes: 9

Related Questions