Python创建、删除桌面、启动组快捷方式的例子分享
一、python创桌面建快捷方式的2个例子
例子一:
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:\python27\python.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
二、使用winshell模块创建、删除桌面、启动组快捷方式
当写好一个应用并发行的时候,我们希望在用户的桌面上建立快捷方式方便用户操作,winshell模块提供了我们需要的功能
下面这个函数将创建程序自身的快捷方式到桌面:
from os import path
import winshell
def create_shortcut_to_desktop():
target = argv[0]
title = '我的快捷方式'
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)
下面这个函数实现将本程序的快捷方式从桌面删除:
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)
下面这个函数实现了建立快捷方式到启动组:
from os import path
import winshell
def create_shortcut_to_startup():
target = argv[0]
title = '我的快捷方式'
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)