One, python create desktop shortcuts 2 examples
Example one:
Copy Code code as follows:
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
& nbsp; 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:\Python27\python.exe", "Mypython")
Example two:
First have to install ActiveState ActivePython. Because it's got a WinShell library.
Copy Code code as follows:
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
Second, use WinShell module to create, delete desktop, start group shortcuts
When writing an application and release, we want to create shortcuts on the user's desktop to facilitate user action, WinShell module provides the functions we need
The following function creates a shortcut to the desktop for the program itself:
Copy Code code as follows:
From OS import path
Import WinShell
Def create_shortcut_to_desktop ():
target = argv[0]
title = ' My Shortcuts '
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)
The following function enables you to remove shortcuts for this program from the desktop:
Copy Code code as follows:
Def delete_shortcut_from_startup ():
target = argv[0]
s = path.basename (target)
FName = Path.splitext (s) [0]
Delfile = Path.join (Winshell.startup (), fname + '. Lnk ')
Winshell.delete_file (Delfile)
The following function implements the setting up of shortcuts to the Startup group:
Copy Code code as follows:
From OS import path
Import WinShell
Def create_shortcut_to_startup ():
target = argv[0]
title = ' My Shortcuts '
s = path.basename (target)
FName = Path.splitext (s) [0]
WinShell. CreateShortcut (
Path = Path.join (Winshell.startup (),
FName + '. lnk '),
target = target,
icon= (target, 0),
Description=title)