Search code examples
pythonwindowsshortcut

how to create a windows shortcut to a folder in python


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


Solution

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