使用 Python 修改 Windows 快捷方式 [英] Modify Windows shortcuts using Python

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

问题描述

如何使用 Python 更改 Windows 快捷方式?

How do you change a Windows shortcut using Python?

例如来自:

H:\My Music\some_file.mp3

到:

D:\Users\Myself\My Music\some_file.mp3

推荐答案

这是在 Python 中使用 Winshell 库执行此操作的另一种更合适的方法:使用 Python 创建 Windows 快捷方式.在您的情况下,代码将如下所示:

Here's another, more appropriate way to do this in Python with Winshell library: Using Python to create Windows shortcuts. In your case the code will look like:

import os, winshell
from win32com.client import Dispatch

desktop = winshell.desktop()
path = os.path.join(desktop, "some_file.mp3.lnk")
target = r"D:\Users\Myself\My Music\some_file.mp3"
wDir = r"D:\Users\Myself\My Music"
icon = r"D:\Users\Myself\My Music\some_file.mp3"

shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()

应删除或重​​写现有快捷方式.如果您需要它来批处理快捷方式文件,那么我认为有一些方法可以从现有快捷方式中读取路径,但没有找到.

Existing shortcut should be deleted or rewritten. If you need it for batch processing of shortcut files then I think there's some way to read paths from existing shortcuts, but didn't managed to find it.

这篇关于使用 Python 修改 Windows 快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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