python 生成Windows快捷方式
阿新 • • 發佈:2022-05-28
此處以虛擬機器映象快捷方式為例
link_filepath:快捷方式的生成路徑
win32_cmd:需要執行的應用程式
arg_str:exe的引數
快捷方式的目標:由win32_cmd + arg_str組成
icon_location:快捷方式的圖示位置(預設為ico)
working_directory:快捷方式的起始位置
import pythoncom, winshell def generate_shortcut(shortcut_name): """ 生成快捷方式 :vm_name:建立的快捷方式名稱,預設為vm_name """ try: # 在多執行緒時需要新增init,告訴Windows需要開一個com物件 pythoncom.CoInitialize() link_filepath = os.path.join(winshell.desktop(), f"{shortcut_name}.lnk") if os.path.exists(link_filepath): os.remove(link_filepath) win32_cmd = VBOX_MANAGER_EXE_PATH arg_str = f"startvm {vm_name} --type separate" my_working = os.path.abspath(os.path.join(VBOX_MANAGER_EXE_PATH, "..")) with winshell.shortcut(link_filepath) as link: link.path = win32_cmd link.description = "description" link.arguments = arg_str link.icon_location = (ICON_PATH, 0) link.working_directory = my_working pythoncom.CoUninitialize() except Exception as e: logger.info(f"generate_shortcut fail:{e}")