Example of Python creating desktop shortcuts

Source: Internet
Author: User
Tags join


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.