python中sendto_python sendto(右键发送文件到执行的bat)功能的实现

**********************实现右键功能**********************

程序名称:install.py

环境win10+python3

安装库:pip install winshell

#传递参数为xxx.py(1 非全路径名称;2 需要和install.py同级目录)

#生成xxx_bat.bat文件,

# 并且在......\AppData\Roaming\Microsoft\Windows\SendTo下生成xxx_bat右键快捷图标

使用方法:任何文件右键选择send to找到xxx_bat执行

#coding:utf-8

#python:3

import sys

import os

from os import path

import winshell

PYTHON_NAME = sys.argv[1]

# 测试完成

def delete_shortcut_from_sendto(argv):

target = argv

s = path.basename(target)

fname = path.splitext(s)[0]

delfile = path.join(winshell.sendto(), fname + '.lnk')

print(delfile)

winshell.delete_file(delfile)

# 测试完成

def create_shortcut_to_sendto(argv):

target = argv

title = 'test'

s = path.basename(target)

fname = path.splitext(s)[0]

path_ = path.join(winshell.sendto(), fname + '.lnk')

print(path_)

if os.path.isfile(path_):

print("{} exists".format(path_))

delete_shortcut_from_sendto(target)

winshell.CreateShortcut(

Path=path_,#右键sendto快捷方式

Target=target,

Icon=(target, 0),

Description=title)

def Change(python_name):

new_path = os.path.dirname(os.path.realpath(__file__))

new_cmd = "python {}\\{} %1".format(new_path, python_name)

bat_name = os.path.splitext(PYTHON_NAME)[0]

bat_path = "{}\\{}_bat.bat".format(new_path, bat_name)

if not os.path.isfile(bat_path):

with open(bat_path, "w") as f:

f.write(new_cmd)

return bat_path

with open(bat_path, "r") as f:

line = f.readline()

if line == new_cmd:

print("same")

return bat_path

with open(bat_path, "w") as f:

f.write(new_cmd)

return bat_path

if __name__ == "__main__":

bat_path = Change(sys.argv[1])

bat_name = os.path.split(bat_path)[1]

try:

delete_shortcut_from_sendto(bat_path)

except:

print("{}.bat not find; pass".format(bat_path))

create_shortcut_to_sendto(bat_name)

# os.system("pause")

#功能上有细微差别,有不能实现该功能可以自己调试或者给我相关需求我来调试均可

#个人习惯上使用pycharm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值