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? Commented Mar 31, 2020 at 5:09
  • i have tried os.symlink() Commented Mar 31, 2020 at 5:24

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.