简体   繁体   English

Python获取windows临时目录完整路径

[英]Python get windows temp directory full path

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.这是相对简单的问题,但由于某种原因,我找不到通过 python 获取 windows temp 目录完整路径的方法,或者找到已在堆栈溢出社区中发布的类似请求。

I'm using tempfile.gettempdir() command but it seems there is no way to capture the full path and it returns the short version:我正在使用 tempfile.gettempdir() 命令,但似乎无法捕获完整路径并返回简短版本:

'C:\\Users\\SVETLO~1\\AppData\\Local\\Temp' '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\\ C:\\Users\\SvetlozarDraganov\\AppData\\Local\\Temp\\

Does anyone knows how to get the full absolute path to windows-temp folder with python?有谁知道如何使用python获取windows-temp文件夹的完整绝对路径?

Thanks in advance.提前致谢。 Svet斯维特

Edit-1:编辑-1:

I'm testing the %fA suggestion from CMD but for some reason, it doesn't work.我正在测试来自 CMD 的 %fA 建议,但由于某种原因,它不起作用。 If I'm using %sA attribute it actually returns the short path version:如果我使用 %sA 属性,它实际上返回短路径版本:

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

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

The %fA attribute however doesn't return the full path:但是 %fA 属性不返回完整路径:

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

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

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. “短版本”是存在于 VFAT 之前的 8.3 文件名和“完整路径”,因为它是“长文件名”或 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.在 Windows 命令提示符下,您可以使用参数扩展将短转换为长并返回。 See the solution here for an example (replace %~sA in the answer with %~fA in your case).请参阅此处的解决方案以获取示例(在您的情况下将答案中的%~sA替换为%~fA )。 So you can make use of that and call the command with subprocess.run() to read the output.所以你可以利用它并使用subprocess.run()调用命令来读取输出。

I can't recall Python has a built-in function for doing this conversion.我不记得 Python 有一个内置函数来进行这种转换。 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.但是Windows API确实如此:您可能想看看这个将 LFN 转换为短文件名的解决方案,在您的情况下,该函数应该是GetLongPathName()

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我在 python os 模块中找到了一个开箱即用的解决方案:https ://docs.python.org/2/library/os.path.html#os.path.realpath

It returns the long-version path!它返回长版本路径!

Thanks to everybody!感谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM