使用Python在Windows、Linux、MacOS系统创建快捷方式

一、使用Python在Windows系统创建快捷方式

Windows系统需要安装 winshell

bash 复制代码
pip3 install winshell

使用winshell.CreateShortcut函数来创建快捷方式,有两个参数:path和target,

path:快捷方式文件的路径,

target:快捷方式指向的目标文件路径

python 复制代码
import os
import sys
import platform

def create_shortcut(target, shortcut_location):

    system = platform.system()

    if system == 'Windows':

        # 在Windows下创建快捷方式
        import winshell

        winshell.CreateShortcut(Path=shortcut_location, Target=target, Icon=(target,0))
   
create_shortcut('/root/pictures/airship.png', '/root/pictures/abc.desktop')

二、使用Python在Linux系统创建快捷方式

Linux:需要创建一个桌面入口文件(.desktop文件)来实现快捷方式

python 复制代码
import os
import sys
import platform

def create_shortcut(target, shortcut_location):

    system = platform.system()

    if system == 'linux':
     
        # 在Linux下创建快捷方式/root/pictures
        from pathlib import Path

        # 将target处理完绝对路径
        target = Path(target).resolve()

        with open(shortcut_location, 'w') as f:
            f.write(f'[Desktop Entry]\nType=Link\nURL={target}\nName={target.name}\n')

create_shortcut('/root/pictures/airship.png', '/root/pictures/abc.desktop')

三、使用Python在MacOS系统创建快捷方式

MacOS:需要创建一个bash脚本实现快捷方式功能,需要使用chmod函数让该脚本可执行

python 复制代码
import os
import sys
import platform

def create_shortcut(target, shortcut_location):

    system = platform.system()

    if system == 'Darwin':

        # 在macOS下创建快捷方式
        from pathlib import Path

        # 将target处理完绝对路径
        target = Path(target).resolve()

        with open(shortcut_location, 'w') as f:
            f.write(f'#!/bin/sh\nopen "{target}"\n')

        os.chmod(shortcut_location, 0x755)

create_shortcut('/root/pictures/airship.png', '/root/pictures/abc.desktop')
相关推荐
晨曦5432102 小时前
函数和模式化——python
开发语言·python
Verdure陌矣3 小时前
游戏开发中 C#、Python 和 C++ 的比较
c++·python·游戏·c#
暮云星影3 小时前
十二、buildroot系统 adb登录权限设置
linux·arm开发·adb
HHONGQI1233 小时前
Linux 基础入门操作 前言 VIM的基本操作 2
linux·运维·服务器·vim
丰锋ff4 小时前
借助 AI 工具使用 Python 实现北京市店铺分布地理信息可视化教程
人工智能·python·信息可视化
java1234_小锋4 小时前
一周学会Pandas2 Python数据处理与分析-Pandas2二维数据结构-DataFrame
数据结构·python·pandas
qq_543248524 小时前
Linux网络配置与测试
linux·运维·网络
钡铼技术物联网关4 小时前
下一代楼宇自控的中枢神经:ARM终端的生态
大数据·linux·人工智能
依旧风轻4 小时前
深入理解 rsync daemon 模式(守护进程)
linux·ios·rsync·daemon·sqi
孙桂月5 小时前
Python爬取数据(二)
开发语言·python