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
Asked
Active
Viewed 4,555 times
3
-
What have you tried so far? – Klaus D. Mar 31 '20 at 05:09
-
i have tried os.symlink() – Soheil Rasouli Mar 31 '20 at 05:24
1 Answers
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
-
3This 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. – Ryan Armstrong Feb 04 '22 at 11:43
-
1Also 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: https://learn.microsoft.com/en-us/previous-versions/fywyxt64(v=vs.85) – Ryan Armstrong Feb 04 '22 at 12:05