coc*_*o4l 5 python wsh shortcut
我正在尝试通过python创建一个快捷方式,它将在带有参数的另一个程序中启动一个文件.例如:
"C:\file.exe" "C:\folder\file.ext" argument
Run Code Online (Sandbox Code Playgroud)
我试过的代码搞砸了:
from win32com.client import Dispatch
import os
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = r'"C:\file.exe" "C:\folder\file.ext"'
shortcut.Arguments = argument
shortcut.WorkingDirectory = "C:\" #or "C:\folder\file.ext" in this case?
shortcut.save()
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个错误:
AttributeError: Property '<unknown>.Targetpath' can not be set.
Run Code Online (Sandbox Code Playgroud)
我尝试过不同格式的字符串,谷歌似乎并不知道这个问题的解决方案
from comtypes.client import CreateObject
from comtypes.gen import IWshRuntimeLibrary
shell = CreateObject("WScript.Shell")
shortcut = shell.CreateShortCut(path).QueryInterface(IWshRuntimeLibrary.IWshShortcut)
shortcut.TargetPath = "C:\file.exe"
args = ["C:\folder\file.ext", argument]
shortcut.Arguments = " ".join(args)
shortcut.Save()
Run Code Online (Sandbox Code Playgroud)