3

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

2
  • What have you tried so far?
    – Klaus D.
    Mar 31, 2020 at 5:09
  • i have tried os.symlink() Mar 31, 2020 at 5:24

1 Answer 1

4

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

2
  • 3
    This example doesn't actually use winshell, it just uses win32com to ask WScript to do it. If you want to use winshell, you should create winshell.Shorcut(). So, in summary, you can use either winshell or pywin32, but you don't need both. Feb 4, 2022 at 11:43
  • 1
    Also note: if you need parameters in this method, you can't append them to the Targetpath (regardless of how the dialog works). You need to assign to .Arguments (e.g. shortcut.Arguments = '--help'). See the core Windows Script documentation for details and a few other options: learn.microsoft.com/en-us/previous-versions/fywyxt64(v=vs.85) Feb 4, 2022 at 12:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.