当前位置:   article > 正文

Python快捷键创建文件、文件夹_python创建快捷方式

python创建快捷方式

一、原理

原理:根据PyHook3插件监控键盘按键,触发函数里面创建文件、文件夹。

二、安装插件

PyHook3、pythoncom、win32gui、getpass插件安装
这里就不赘述了。

三、文件名:程序外挂-快捷创建文件、文件夹.py

文件名:程序外挂-快捷创建文件、文件夹.py

四、上代码

程序外挂-快捷创建文件、文件夹.py

# -*- coding: utf-8 -*-
import pythoncom
import PyHook3
import win32gui
import time
import getpass

# 得到日期格式
def get_date_string_file():
    return time.strftime("%Y%m%d_%H%M%S",time.localtime())

def get_date_string_info():
    return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

# 在制定目录创建文件
def create_text_file(dir):
    file_name = get_date_string_file() + '.sql'
    file_full_path = dir + '/' + file_name
    file = open(file_full_path,'w')
    file.close()
    return file_full_path

# 创建文件夹
def mkdir(path):
    # 引入模块
    import os
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)
        # print(path + ' 创建成功')
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path + ' 目录已存在')
        return False

# PyHook3监控键盘
def OnKeyboardEvent(event):
    # print(event.Key)
    # 如果按下的是右边的Ctrl键
    if event.Key == 'Rcontrol':
        window = win32gui.GetForegroundWindow()
        if(window!=0):
            if(win32gui.GetClassName(window)=='CabinetWClass'):
                # 得到目录完整路径
                dir = win32gui.GetWindowText(window)
                # 创建文本文件
                file_full_path = create_text_file(dir)
                print(get_date_string_info()+' [INFO] : '+'该窗口为目录。文件创建成功:'+file_full_path)
            else:
                #getpass.getuser()得到当前用户名
                dir = 'c:/Users/'+getpass.getuser()+'/Desktop'
                file_full_path = create_text_file(dir)
                print(get_date_string_info()+' [INFO] : '+'该窗口不为目录。桌面文件创建成功:'+file_full_path)
    if event.Key == 'F9':
        window = win32gui.GetForegroundWindow()
        if(window!=0):
            if(win32gui.GetClassName(window)=='CabinetWClass'):
                # 得到目录完整路径
                dir = win32gui.GetWindowText(window)
                # 创建文件夹
                file_full_path = dir + '/' + get_date_string_file() + '-'
                result =  mkdir(file_full_path)
                if(result == True):
                    print(get_date_string_info() + ' [INFO] : ' + '√ 该窗口为目录。文件夹创建成功:' + file_full_path)
                else:
                    print(get_date_string_info() + ' [INFO] : ' + '× 该窗口为目录。文件夹创建失败:' + file_full_path)
            else:
                #getpass.getuser()得到当前用户名
                dir = 'c:/Users/'+getpass.getuser()+'/Desktop'
                file_full_path = dir + '/' + get_date_string_file() + '-'
                result = mkdir(file_full_path)
                if (result == True):
                    print(get_date_string_info() + ' [INFO] : ' + '√ 该窗口不为目录。桌面文件夹创建成功:' + file_full_path)
                else:
                    print(get_date_string_info() + ' [INFO] : ' + '× 该窗口不为目录。桌面文件夹创建失败:' + file_full_path)
    return True

# 创建钩子管理者
hm = PyHook3.HookManager()

# 注册键盘回调事件
hm.KeyDown = OnKeyboardEvent

# 钩住键盘
hm.HookKeyboard()

if __name__ == '__main__':
    pythoncom.PumpMessages()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

五、代码分析

主要是用PyHook3插件的监控键盘的功能,监控键盘按键,
win32gui得到目录完整路径,然后创建文件。

六、打包成.exe

指令:

pyinstaller -F -i py.ico 程序外挂-快捷创建文件、文件夹.py
  • 1

七、操作说明:

7.1、按右Ctrl键创建文件。

如果当前焦点在桌面或不在文件夹,在桌面上创建文件。否则在当前焦点的文件夹内创建文件。
文件命名规范。例如:20210830_230416.sql

7.2、按F9创建文件夹

如果当前焦点在桌面或不在文件夹,在桌面上创建文件夹。否则在当前焦点的文件夹内创建文件夹。
文件夹命名规范。例如:20210830_230416-

八、.py 文件 网盘下载地址

.py 文件 网盘下载链接:https://pan.baidu.com/s/1ozR7kQUw7evTBZjn7NAYFQ?pwd=yyds

九、.exe 文件 网盘下载地址

.exe 文件 网盘下载链接:https://pan.baidu.com/s/1glNXuU56MI9ng9x7_vIUrQ?pwd=yyds

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/264801?site
推荐阅读
相关标签
  

闽ICP备14008679号