一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python创建桌面快捷方式的例子

时间:2014-03-20 编辑:简简单单 来源:一聚教程网


参考了一下网上的办法,简化了一下。

方法二

 代码如下 复制代码

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
    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")

方法二

首先得安装 ActiveState ActivePython  . 因为这个中带了 winshell 库

 代码如下 复制代码

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)

注:不支持win64

热门栏目