首页 > 解决方案 > 使用python3在Windows中创建文件的快捷方式(.lnk)

问题描述

我想在某些特定路径中创建某些文件的快捷方式(.lnk)。例如在 ("H:\happy\hi\new.lnk") 中创建我的文件("D:\New folder\new.exe") 的快捷方式,我想在 python3 中编写这个程序

标签: pythonpython-3.xwindowsshortcutlnk

解决方案


首先,安装需求

pip install pywin32
pip install winshell

那么这就是你必须编写的代码。

import os, winshell
from win32com.client import Dispatch

path = r"H:\happy\hi\new.lnk"  # Path to be saved (shortcut)
target = r"D:\New folder\new.exe"  # The shortcut target file or folder
work_dir = r"D:\New folder"  # The parent folder of your file

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

更多详情:https ://winshell.readthedocs.io/en/latest/shortcuts.html


推荐阅读