无法启动 Windows 快捷方式 [英] Unable to launch windows shortcut
问题描述
我正在尝试用 python 启动一个窗口.我已经尝试了 os.system、subprocess.call、os.startfile 等多种方法,但我总是收到一条错误消息,指出路径不存在.
I'm trying to launch a windows with python. I've tried NUMEROUS approaches with os.system, subprocess.call, os.startfile etc. but I'm always getting an error saying that the path does not exist.
我知道路径是正确的,因为我已经尝试在 CMD.EXE 中运行以下命令:
I know that the path is correct because I've tried running the following command in CMD.EXE:
start D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk
以下是我尝试过但没有成功的一些方法:
Here is some of the stuff I've tried without success:
os.startfile(r"D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk")
os.startfile("D:\\johan\\programmering\\Scripts\\shortcuts\\HWMonitor.lnk")
subprocess.call("D:\\johan\\programmering\\Scripts\\shortcuts\\HWMonitor.lnk")
subprocess.call(r"D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk")
subprocess.Popen(r"D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk")
subprocess.Popen(r"D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk", shell=True)
os.system(r"start D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk")
p= subprocess.Popen(r"start D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk", shell=True)
p.wait()
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r"D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk")
subprocess.call(shortcut.Targetpath)
免责声明我知道在 SO 上有类似的问题,但没有一个对我有帮助.所以在你开始喊重复!"之前请注意,我已经尝试过这些解决方案但没有成功.
DISCLAIMER I know that there are similar questions asked on SO, but none of them have helped me. So before you start crying "duplicate!" please know that I've tried the solutions with no success.
推荐答案
根据这个 answer,你可以解析你的链接路径,然后调用解析的路径
According to this answer, you could resolve your link path, then call the resolved path
import sys
import win32com.client,win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r"D:\johan\programmering\Scripts\shortcuts\HWMonitor.lnk")
long_path = shortcut.Targetpath
但是 long_path
可能是一个奇怪的 Windows 路径,里面有很多垃圾,所以如果
but long_path
may be a strange Windows path with a lot of junk in it, so if
subprocess.call([long_path])
不行,可以用短路径(8.3名称)解析长路径:
doesn't work, you can resolve the long path in a short path (8.3 names):
short_path=win32api.GetShortPathName(long_path)
现在做:
subprocess.call([short_path])
这篇关于无法启动 Windows 快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!