吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 667|回复: 8
上一主题 下一主题
收起左侧

[Python 原创] 根据关键字查找文件工具

[复制链接]
跳转到指定楼层
楼主
LiCan857 发表于 2024-8-14 09:23 回帖奖励
不知道这种工具之前有没有人写过。
用python写了个根据关键字找文件的工具,做成了GUI界面。
对于只记得文件部分内容,但找不到文件在哪里的很有帮助。




[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import filedialog, messagebox
import threading
import pandas as pd
import os
import sys


# 自定义控制台输出到GUI
class RedirectText:
    def __init__(self, text_widget):
        self.text_widget = text_widget

    def write(self, string):
        self.text_widget.insert(tk.END, string)
        self.text_widget.see(tk.END)  # 自动滚动到最后一行

    def flush(self):
        pass


def find_key(directory, keyword, process_button, check_var, check_btn):
    found_files = False
    num = 0
    for root, dirs, files in os.walk(directory):
        for filename in files:
            if check_var.get():
                if filename.endswith('.xls'):
                    file_path = os.path.join(root, filename)
                    index_excel = find_key_in_excel(file_path, keyword, 'xls')
                    for i in range(len(index_excel)):
                        num = num + 1
                        print(f"[{num}] " + file_path + f": " + str(index_excel[i]))
                        found_files = True
                        print("******************************************\n")
                if filename.endswith('.xlsx'):
                    file_path = os.path.join(root, filename)
                    index_excel = find_key_in_excel(file_path, keyword, 'xlsx')
                    for i in range(len(index_excel)):
                        num = num + 1
                        print(f"[{num}] " + file_path + f": " + str(index_excel[i]))
                        found_files = True
                        print("******************************************\n")
            if filename.endswith(('.py', '.txt', 'json', 'md', '.c', '.cpp', '.h', '.java', '.js', '.html', '.css',
                                  '.xml', '.sql', '.bat', '.ps1', '.sh', '.yaml', '.yml', '.conf', '.ini', '.license')):
                file_path = os.path.join(root, filename)
                try:
                    with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
                        content_lines = file.readlines()
                        for line_number, line in enumerate(content_lines, start=1):
                            if keyword in line:
                                num = num + 1
                                print(f"[{num}] " + file_path + f": " + line.strip(), f"({line_number})")
                                found_files = True
                                print("******************************************\n")
                except IOError as e:
                    print(f"文件读取错误 {file_path}: {e}")
                    process_button.config(state='normal')
                    check_btn.config(state='normal')
    if not found_files:
        print(f"在'{directory}'下没有找到关键字为'{keyword}'的文件...")
    process_button.config(state='normal')
    check_btn.config(state='normal')


def find_key_in_excel(file_path, key, typ):
    try:
        if typ == 'xlsx':
            sheets = pd.read_excel(file_path, sheet_name=None, engine='openpyxl',
                                   header=None)  # 设置header=None以不将第一行视为表头
        else:
            sheets = pd.read_excel(file_path, sheet_name=None, engine='xlrd', header=None)  # 设置header=None以不将第一行视为表头
    except Exception as e:
        print(f"文件读取错误 {file_path}: {e}")
        return

    index_list = []
    # 遍历DataFrame的所有单元格
    try:
        for sheet_name, sheet in sheets.items():
            for i in range(sheet.shape[0]):  # 行数
                for j in range(sheet.shape[1]):  # 列数
                    if key in str(sheet.iat[i, j]):  # 比较单元格中的值是否等于key
                        row_num = i + 1  # Excel的行号是从1开始的
                        col_num = j + 1  # Excel的列号也是从1开始的
                        index_list.append([sheet_name, row_num, col_num])
        return index_list
    except IOError as e:
        print(f"文件读取错误 {file_path}: {e}")
        return


def select_directory(entry_widget):
    directory = filedialog.askdirectory()
    if directory:
        entry_widget.delete(0, tk.END)
        entry_widget.insert(0, directory)


# 软件详情
def show_info():
    info_text = ("Author: 灿\n"
                 "编译时间版本: 2024-08-12/ V-0.01\n"
                 "说明:读取指定目录下的文件,查找文件中包含的指定关键词,并输出到控制台。\n"
                 "支持文件类型:.py, .txt, json, md, .c, .cpp, .h, .java, .js, .html, .css, .xml, .sql, .bat, .ps1, .sh, .yaml, .yml, .conf, .ini, .license, .xls, .xlsx")
    messagebox.showinfo("软件信息", info_text)


def update_gui(button):
    """在主线程中更新 GUI"""
    button.config(state='normal')  # 任务完成后重新启用按钮


def start_task(process_button, path_entry, keyword_entry, check_var, check_btn):
    """启动一个新线程来执行耗时任务,并禁用按钮"""
    process_button.config(state='disabled')  # 在开始任务前禁用按钮
    check_btn.config(state='disabled')
    thread = threading.Thread(target=lambda: find_key(path_entry.get(), keyword_entry.get(), process_button, check_var, check_btn))
    thread.start()


def create_gui():
    root = tk.Tk()
    root.title("文件查找工具")
    root.geometry("600x500")
    root.resizable(width=False, height=False)  # 不可改变窗口大小

    # 设置窗口图标
    # root.iconbitmap('../fish.ico')

    # 路径选择部分
    label = tk.Label(root, text="请选择文件夹路径:")
    label.place(x=10, y=10, width=120, height=30)

    # 路径
    path_entry = tk.Entry(root, width=40)
    path_entry.place(x=140, y=10, width=350, height=30)

    browse_button = tk.Button(root, text="浏览", command=lambda: select_directory(path_entry))
    browse_button.place(x=500, y=10, width=50, height=30)

    label = tk.Label(root, text="关键字:")
    label.place(x=10, y=50, width=120, height=30)

    # 关键字输入框
    keyword_entry = tk.Entry(root, width=40)
    keyword_entry.place(x=140, y=50, width=200, height=30)

    check_var = tk.IntVar()
    check_btn = tk.Checkbutton(root, text="是否检索excel(勾选将会增加检索时间)", variable=check_var)
    check_btn.place(x=350, y=50, width=240, height=30)

    process_button = tk.Button(root, text="开始查找",
                               command=lambda: start_task(process_button, path_entry, keyword_entry, check_var, check_btn))
    process_button.place(x=250, y=90, width=80, height=30)

    # 信息按钮
    info_button = tk.Button(root, text="软件信息", command=show_info)
    info_button.place(x=250, y=130, width=80, height=30)

    # 控制台输出区域
    console_frame = tk.Frame(root)
    console_frame.place(x=10, y=170, width=580, height=320)

    text_console = tk.Text(console_frame, wrap='word', height=20)
    text_console.place(x=10, y=0, width=580, height=320)

    # 将 sys.stdout 重定向到 text_console
    redirect_text = RedirectText(text_console)
    sys.stdout = redirect_text

    root.mainloop()


if __name__ == '__main__':
    create_gui()

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
hly2233 发表于 2024-8-14 19:13
如果是当工具,推荐直接用everything
推荐
开创者 发表于 2024-8-14 10:23
不太好用,估计文件多了,出结果后直接卡死了
3#
 楼主| LiCan857 发表于 2024-8-14 10:29 |楼主
开创者 发表于 2024-8-14 10:23
不太好用,估计文件多了,出结果后直接卡死了

尽量选择大概的文件夹,不要直接选根目录
4#
52soft 发表于 2024-8-14 10:47
我来测试一下
5#
Pwaerm 发表于 2024-8-14 12:22
window自带这样的功能呀

谢谢楼主分享,可以作为学习资料
6#
chenyong2020 发表于 2024-8-14 12:49
不知道具体使用情况咋样,但愿好用。
8#
mytomsummer 发表于 2024-8-16 13:14
进步空间很广阔!
9#
long88888888 发表于 2024-8-19 21:35
建议还是直接用主流成熟的软件,毕竟现在这种很多很稳定。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-21 09:52

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表