Home > Software engineering >  how to create a windows shortcut to a folder in python
how to create a windows shortcut to a folder in python

Time:10-17

My problem is that i can't find a solution to create a shortcut to an folder with python. Only to a file, code example:

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

But I need a shortcut to a whole folder.

My target example is:

C:/Usersand C:/Users/user/Downloads

CodePudding user response:

You can use the below code to create shortcut to a directory

from win32com.client import Dispatch

path = r"C:\Users\user\Downloads\shortcut.lnk"  #This is where the shortcut will be created
target = r"C:\Users\user\Downloads" # directory to which the shortcut is created

shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.save()
  • Related