在 Python 中读取 .lnk 文件的目标? [英] Reading the target of a .lnk file in Python?
问题描述
我正在尝试从 Python 读取快捷方式 (.lnk
) 文件的目标文件/目录.有没有头疼的方法来做到这一点?规格超出了我的脑海.我不介意使用仅限 Windows 的 API.
I'm trying to read the target file/directory of a shortcut (.lnk
) file from Python. Is there a headache-free way to do it? The spec is way over my head.
I don't mind using Windows-only APIs.
我的最终目标是在 Windows XP 和 Vista 上找到 (My) Videos"
文件夹.在 XP 上,默认情况下,它位于 %HOMEPATH%My DocumentsMy Videos
,而在 Vista 上,它位于 %HOMEPATH%Videos
.但是,用户可以重新定位此文件夹.在这种情况下,%HOMEPATH%Videos
文件夹不再存在,而是由 %HOMEPATH%Videos.lnk
替换,它指向新的 "My视频"
文件夹.我想要它的绝对位置.
My ultimate goal is to find the "(My) Videos"
folder on Windows XP and Vista. On XP, by default, it's at %HOMEPATH%My DocumentsMy Videos
, and on Vista it's %HOMEPATH%Videos
. However, the user can relocate this folder. In the case, the %HOMEPATH%Videos
folder ceases to exists and is replaced by %HOMEPATH%Videos.lnk
which points to the new "My Videos"
folder. And I want its absolute location.
推荐答案
使用 Python 创建快捷方式(通过 WSH)
import sys
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("t:\test.lnk")
shortcut.Targetpath = "t:\ftemp"
shortcut.save()
使用 Python(通过 WSH)读取快捷方式的目标
import sys
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("t:\test.lnk")
print(shortcut.Targetpath)
这篇关于在 Python 中读取 .lnk 文件的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!