python打开快捷方式_Python创建启动目录的快捷方式,python,到
# -*- coding:utf-8 -*-
# author:lizonezhi
import os
import sys
import pythoncom
import win32com.client as client
def createShortCut(filename): # 目前創(chuàng)建的無(wú)起始位置
"""filename should be abspath, or there will be some strange errors"""
try:
# 設(shè)置快捷方式的起始位置,此處設(shè)置為windows啟動(dòng)目錄
working_directory = os.getenv(
'USERPROFILE') + '\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\\'
# 創(chuàng)建快捷方式的目標(biāo)絕對(duì)路徑
lnkname = working_directory + filename + '.lnk'
# 要?jiǎng)?chuàng)建快捷方式的文件的絕對(duì)路徑,此處是獲取當(dāng)前路徑
filename = os.path.dirname(os.path.realpath(sys.argv[0])) + '\\' + filename
shortcut = client.Dispatch("WScript.Shell").CreateShortCut(lnkname)
shortcut.TargetPath = filename
shortcut.save()
print('配置開(kāi)機(jī)自啟')
except Exception as e:
print(e.args)
def set_shortcut(filename): # 如無(wú)需特別設(shè)置圖標(biāo),則可去掉iconname參數(shù)
try:
from win32com.shell import shell
from win32com.shell import shellcon
iconname = ""
# 設(shè)置快捷方式的起始位置,此處設(shè)置為windows啟動(dòng)目錄
working_directory = os.getenv(
'USERPROFILE') + '\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\\'
# 創(chuàng)建快捷方式的目標(biāo)絕對(duì)路徑
lnkname = working_directory + filename + '.lnk'
# 要?jiǎng)?chuàng)建快捷方式的文件的絕對(duì)路徑,此處是獲取當(dāng)前路徑
filename = os.path.dirname(os.path.realpath(sys.argv[0])) + '\\' + filename
shortcut = pythoncom.CoCreateInstance(
shell.CLSID_ShellLink, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
shortcut.SetPath(filename)
# 設(shè)置快捷方式的起始位置, 不然會(huì)出現(xiàn)找不到輔助文件的情況
shortcut.SetWorkingDirectory(working_directory)
# 可有可無(wú),沒(méi)有就默認(rèn)使用文件本身的圖標(biāo)
shortcut.SetIconLocation(iconname, 0)
if os.path.splitext(lnkname)[-1] != '.lnk':
lnkname += ".lnk"
shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname, 0)
return True
except Exception as e:
print(e.args)
return False
set_shortcut('test.exe')
以上代碼打包為test.exe后,雙擊啟動(dòng),可配置自身開(kāi)機(jī)自啟(exe不能是uac管理員權(quán)限)。或者配置其它程序。
按 WIN + R 輸入?shell:startup 回車打開(kāi)windows啟動(dòng)目錄查看結(jié)果。
參考文章:
總結(jié)
以上是生活随笔為你收集整理的python打开快捷方式_Python创建启动目录的快捷方式,python,到的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Laravel 5 多个视图共享数据的方
- 下一篇: python合并txt文本_Python