使用python 创建快捷方式
生活随笔
收集整理的這篇文章主要介紹了
使用python 创建快捷方式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import os
import pythoncom
from win32com.shell import shell
from win32com.shell import shellcondef set_shortcut(): # 如無需特別設(shè)置圖標(biāo),則可去掉iconname參數(shù)try:filename = r"D:\AppServ\timer\win_cron_zq\timer.exe" # 要?jiǎng)?chuàng)建快捷方式的文件的完整路徑iconname = ""lnkname = r"C:\Users\pc1\Desktop" + r"\timer.exe.lnk" # 將要在此路徑創(chuàng)建快捷方式shortcut = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None,pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)shortcut.SetPath(filename)shortcut.SetWorkingDirectory(r"D:\AppServ\timer\win_cron_zq") # 設(shè)置快捷方式的起始位置, 不然會(huì)出現(xiàn)找不到輔助文件的情況shortcut.SetIconLocation(iconname, 0) # 可有可無,沒有就默認(rèn)使用文件本身的圖標(biāo)if os.path.splitext(lnkname)[-1] != '.lnk':lnkname += ".lnk"shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname, 0)return Trueexcept Exception as e:print(e.args)return False
剛開始沒有設(shè)置shortcut.SetWorkingDirectory(r"D:\AppServ\timer\win_cron_zq")這一步,結(jié)果快捷方式雖然能執(zhí)行,總是提示找不到相應(yīng)的其他相輔相成的文件, 加上這個(gè)起始位置設(shè)置后, 就可以了 ,這里記一下
總結(jié)
以上是生活随笔為你收集整理的使用python 创建快捷方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用python os.system 执行
- 下一篇: python 各个模块的简单介绍