Python创建、删除桌面、启动组快捷方式的例子分享

 更新时间:2014年04月15日 11:40:17   作者:  
这篇文章主要介绍了Python创建、删除桌面、启动组快捷方式的例子分享,需要的朋友可以参考下

一、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)

 

 

 

相关文章

  • shell脚本编程之循环语句学习笔记

    shell脚本编程之循环语句学习笔记

    这篇文章主要介绍了shell脚本编程之循环语句学习笔记,本文内容较简单,可以作为shell循环语句的备忘录,忘记怎么写时来看看吧~需要的朋友可以参考下
    2014-09-09
  • linux shell中Grep命令查找多个字符串(grep同时匹配多个关键字或任意关键字)

    linux shell中Grep命令查找多个字符串(grep同时匹配多个关键字或任意关键字)

    grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来,下面这篇文章主要给大家介绍了关于linux shell中Grep命令查找多个字符串(grep同时匹配多个关键字或任意关键字)的相关资料,需要的朋友可以参考下
    2022-08-08
  • linux shell 逻辑运算符、逻辑表达式详细介绍

    linux shell 逻辑运算符、逻辑表达式详细介绍

    shell的逻辑运算符 涉及有以下几种类型,因此只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果
    2014-02-02
  • shell传参并将参数传递给sql文件的方法

    shell传参并将参数传递给sql文件的方法

    今天小编就为大家分享一篇shell传参并将参数传递给sql文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • Linux中rpm命令用法详解

    Linux中rpm命令用法详解

    rpm命令是用于在基于RPM包管理系统的Linux发行版中安装、查询、升级和删除软件包的命令行工具,本文讲给大家详细的介绍一下Linux rpm命令的使用方法,感兴趣的同学跟着小编一起来看看吧
    2023-08-08
  • Shell脚本实现FTP自动上传和下载文件

    Shell脚本实现FTP自动上传和下载文件

    本文主要介绍了Shell脚本实现FTP自动上传和下载文件,主要内容包括批量下载脚本代码、下载单个文件脚本代码、登录FTP实现上传文件功能、上传单个文件脚本代码等
    2023-08-08
  • Obsidian斜杠命令用法详解

    Obsidian斜杠命令用法详解

    这篇文章主要介绍了终于定制出顺手的Obsidian斜杠命令,利用斜杠命令 + 命令面板置顶 + Quick Add插入内容 + Quick Add别名命令,就可以根据自己习惯,实现定制化的斜杠命令,需要的朋友可以参考下
    2022-12-12
  • awk实现Left、join查询、去除重复值以及局部变量讲解例子

    awk实现Left、join查询、去除重复值以及局部变量讲解例子

    这篇文章主要介绍了awk实现Left、join查询、去除重复值以及局部变量讲解例子,awk的高级使用技巧,需要的朋友可以参考下
    2014-07-07
  • Shell正则表达式之grep、sed、awk实操笔记

    Shell正则表达式之grep、sed、awk实操笔记

    这篇文章主要介绍了Shell正则表达式之grep、sed、awk实操笔记,本文使用grep、sed、awk配合正则达到了一些需求和目的,需要的朋友可以参考下
    2014-09-09
  • Shell脚本break和continue命令简明教程

    Shell脚本break和continue命令简明教程

    这篇文章主要介绍了Shell脚本break和continue命令简明教程,break和continue命令用来在未达到循环结束条件时强制跳出循环,需要的朋友可以参考下
    2014-07-07

最新评论