简体   繁体   中英

Unable to launch windows shortcut

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.

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)

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.

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

but long_path may be a strange Windows path with a lot of junk in it, so if

subprocess.call([long_path])

doesn't work, you can resolve the long path in a short path (8.3 names):

short_path=win32api.GetShortPathName(long_path)

now do:

subprocess.call([short_path])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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