在Python中比较两个Windows路径,其中一个包含波浪号

发布于 2024-08-30 18:09:02 字数 475 浏览 2 评论 0原文

我正在尝试在程序中使用 TMP 环境变量。当我询问时,

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

我得到

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

其中包含老式的波形符形式。我无法控制返回路径的函数,就像

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

我的问题是这样的;我想检查该文件是否在我的临时驱动器中,但我找不到比较它们的方法。如何判断这两个 Windows 目录;

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

是一样的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-09-06 18:09:02

这是仅使用标准 Python 库中的 ctypes 的替代解决方案。

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

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
孤城病女 2024-09-06 18:09:02

您将需要来自 http://sourceforge.net/projects/pywin32/ 的 python win32 扩展我使用 ActiveState 打包的 python

它们包含函数 win32file.GetLongPathName ,它将把 8.3 版本转换为完整路径。

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 into the full path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文