Python創(chuàng)建、刪除桌面、啟動組快捷方式的例子分享
一、Python創(chuàng)桌面建快捷方式的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 . 因?yàn)檫@個中帶了 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模塊創(chuàng)建、刪除桌面、啟動組快捷方式
當(dāng)寫好一個應(yīng)用并發(fā)行的時候,我們希望在用戶的桌面上建立快捷方式方便用戶操作,winshell模塊提供了我們需要的功能
下面這個函數(shù)將創(chuàng)建程序自身的快捷方式到桌面:
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)
下面這個函數(shù)實(shí)現(xiàn)將本程序的快捷方式從桌面刪除:
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)
下面這個函數(shù)實(shí)現(xiàn)了建立快捷方式到啟動組:
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)
相關(guān)文章
Idea使用Bashsupport插件編輯Shell腳本的方法
這篇文章主要介紹了Idea插件之——Bashsupport編輯Shell腳本的相關(guān)知識,功能非常齊全有檢查錯誤并且還可以在idea中直接運(yùn)行shell腳本,本教程帶領(lǐng)大家一步步演示插件的安裝和配置,需要的朋友可以參考下2021-05-05
linux shell進(jìn)度條實(shí)現(xiàn)方法
linux shell實(shí)現(xiàn)的一咱進(jìn)度條,代碼很簡單,供大家參考2013-11-11
linux 環(huán)境 mysql寫入中文報(bào)錯
本篇文章主要介紹了linux 環(huán)境 mysql寫入中文報(bào)錯的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
bash提取字符串${string:position:length}的具體使用
本文主要介紹了bash提取字符串${string:position:length}的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Linux系統(tǒng)上實(shí)現(xiàn)定時重啟Tomcat服務(wù)腳本介紹
大家好,本篇文章介紹的是Linux系統(tǒng)上實(shí)現(xiàn)定時重啟Tomcat服務(wù)腳本介紹,感興趣的同學(xué)趕緊來看看,希望可以對你起到幫助2021-11-11
Shell編程之Shell變量學(xué)習(xí)總結(jié)
這篇文章主要介紹了Shell腳本編程中Shell變量的學(xué)習(xí)總結(jié),需要的朋友可以參考下2014-03-03
linux crontab 實(shí)現(xiàn)每秒執(zhí)行的實(shí)例
下面小編就為大家?guī)硪黄猯inux crontab 實(shí)現(xiàn)每秒執(zhí)行的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

