吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1018|回复: 8
收起左侧

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

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




[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
不太好用,估计文件多了,出结果后直接卡死了
 楼主| LiCan857 发表于 2024-8-14 10:29
开创者 发表于 2024-8-14 10:23
不太好用,估计文件多了,出结果后直接卡死了

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

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

本版积分规则

返回列表

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

GMT+8, 2025-4-1 23:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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