Python,使用两个路径和参数创建快捷方式 [英] Python, create shortcut with two paths and argument

查看:29
本文介绍了Python,使用两个路径和参数创建快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 python 创建一个快捷方式,该快捷方式将在另一个带有参数的程序中启动一个文件.例如:

I'm trying to create a shortcut through python that will launch a file in another program with an argument. E.g:

"C:\file.exe" "C:\folder\file.ext" argument

我尝试过的代码:

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()

但是我遇到了一个错误:

But i get an error thrown my way:

AttributeError: Property '<unknown>.Targetpath' can not be set.

我尝试了不同格式的字符串,但谷歌似乎不知道这个问题的解决方案

I've tried different formats of the string and google doesn't seem to know the solution to this problem

推荐答案

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()

参考

这篇关于Python,使用两个路径和参数创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆