Refer to the online approach, simplified a bit.
Method Two
The code is as follows |
Copy Code |
Import OS Import pythoncom From Win32com.shell Import shell From Win32com.shell import Shellcon Def createdesktoplnk (filename,lnkname): shortcut = pythoncom. CoCreateInstance ( shell). Clsid_shelllink, none, pythoncom. Clsctx_inproc_server, Shell. Iid_ishelllink) shortcut. SetPath (filename) if Os.path.splitext (lnkname) [-1]!= '. lnk ': Lnkname +. LNK # Get desktop path &N bsp; DesktopPath = Shell. SHGetPathFromIDList (shell. SHGetSpecialFolderLocation (0,shellcon. csidl_desktop)) lnkname = Os.path.join (desktoppath,lnkname) shortcut. QueryInterface (Pythoncom. Iid_ipersistfile). Save (lnkname,0) if __name__ = = ' __main__ ': Createdesktoplnk (U "C:python27python.exe", "Mypython") |
Method Two
First have to install ActiveState ActivePython. Because it's got a WinShell library.
The code is as follows |
Copy Code |
From OS import path Import WinShell #---------------------------------------------------------------------- def create_shortcut_to_desktop (Target,title): "" "Create Shortcut to Desktop" "" " s = path.basename (target) FName = Path.splitext (s) [0] WinShell. CreateShortcut ( Path = Path.join (Winshell.desktop (), fname + '. lnk '), target = target, icon= (target, 0), Description=title) |
Note: Win64 is not supported