create a shortcut(.lnk) of file in windows with python3

Viewed 2632

i want to create a shortcut(.lnk) of some files in some specific path. for example make shortcut of my file("D:\New folder\new.exe") in ("H:\happy\hi\new.lnk") i want to write this program in python3

1 Answers

first, install the requirements

pip install pywin32
pip install winshell

then this is the code you have to write.

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

for more details: https://winshell.readthedocs.io/en/latest/shortcuts.html

Related