views:

105

answers:

2

I'm trying to use the TMP environment variable in a program. When I ask for

tmp = os.path.expandvars("$TMP")

I get

C:\Users\STEVE~1.COO\AppData\Local\Temp

Which contains the old-school, tilde form. A function I have no control over returns paths like

C:\Users\steve.cooper\AppData\Local\Temp\file.txt

My problem is this; I'd like to check if the file is in my temp drive, but I can't find a way to compare them. How do you tell if these two Windows directories;

C:\Users\STEVE~1.COO\AppData\Local\Temp
C:\Users\steve.cooper\AppData\Local\Temp

are the same?

+3  A: 

You will need the python win32 extensions from http://sourceforge.net/projects/pywin32/ or I use python packaged by ActiveState

They include the function win32file.GetLongPathName which will transform the 8.3 version inot the full path.

Mark
+1  A: 

Here is alternative solution using only ctypes from Standard Python Library.

tmp = unicode(os.path.expandvars("$TMP"))

import ctypes
GetLongPathName = ctypes.windll.kernel32.GetLongPathNameW
buffer = ctypes.create_unicode_buffer(GetLongPathName(tmp, 0, 0))
GetLongPathName(tmp, buffer, len(buffer))
print buffer.value
Constantin