0

This is relatively simple matter but for some reason I cannot find a way to get the full path of the windows temp directory via python or to find similar request already posted in stack overflow community.

I'm using tempfile.gettempdir() command but it seems there is no way to capture the full path and it returns the short version:

'C:\Users\SVETLO~1\AppData\Local\Temp'

This overall works but there is an important part of the script later on which doesn't and it requires the full temp dir path, which is:

C:\Users\SvetlozarDraganov\AppData\Local\Temp\

Does anyone knows how to get the full absolute path to windows-temp folder with python?

Thanks in advance. Svet

Edit-1:

I'm testing the %fA suggestion from CMD but for some reason, it doesn't work. If I'm using %sA attribute it actually returns the short path version:

CMD: for %A in ("C:\Users\SvetlozarDraganov\AppData\Local\Temp") do @echo %~sA

OUTPUT: C:\Users\SVETLO~1\AppData\Local\Temp

The %fA attribute however doesn't return the full path:

CMD: for %A in ("C:\Users\SVETLO~1\AppData\Local\Temp") do @echo %~fA

OUTPUT: C:\Users\SVETLO~1\AppData\Local\Temp

  • If you get `'C:\Users\SVETLO~1\AppData\Local\Temp'`, have you tried using `os.path.expanduser('C:\Users\SVETLO~1\AppData\Local\Temp')`https://docs.python.org/3/library/os.path.html#os.path.expanduser – Flair Nov 10 '20 at 22:55
  • Although this looks like exactly what I'm looking for, it doesn't work as expected. It returns the same string, unfortunately. – Svetlozar Draganov Nov 11 '20 at 19:19

2 Answers2

1

Speaking of full absolute path, you already have it.

The "short version" is the 8.3 filename that exists before VFAT and the "full path" as you meant it is the "long filename" or LFN. They are interchangable so long as accessing the file in concern.

In Windows command prompt, you can use parameter expansions to convert from short to long and back. See the solution here for an example (replace %~sA in the answer with %~fA in your case). So you can make use of that and call the command with subprocess.run() to read the output.

I can't recall Python has a built-in function for doing this conversion. But Windows API does: You may want to see how this solution for converting LFN to short filename, which in your case, the function should be GetLongPathName() instead.

adrtam
  • 6,991
  • 2
  • 12
  • 27
1

I found a out-of-the-box solution to my question in python os module: https://docs.python.org/2/library/os.path.html#os.path.realpath

It returns the long-version path!

Thanks to everybody!