'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:/Users
and C:/Users/user/Downloads
Solution 1:[1]
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()
Solution 2:[2]
Backslashes (\
) must be used in shortcut.Targetpath
for shortcuts to work correctly for folders.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LiquidDeath |
Solution 2 | icaine |