Python建立、刪除案頭、啟動組捷徑的例子分享

來源:互聯網
上載者:User
一、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)

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.