os.startfile 会在关联的应用程序启动后立即返回,因此请改用 Popen。
当您使用 Windows 时,请使用这些步骤。
要在 Windows 上使用 Popen 打开快捷方式,首先安装 pywin32
第一步:
python -m pip install pywin32
第二步:
导航到您的 python Scrips 文件夹,例如 C:\Users\Name\AppData\Local\Programs\Python\Python38-32\Scripts,然后键入命令。
pywin32_postinstall.py -install
然后是使用 Popen 的代码。
import subprocess
import win32com.client, win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r'path to shortcut')
long_path = shortcut.Targetpath
p = subprocess.Popen(long_path)
p.wait()