根据关键字查找文件工具
不知道这种工具之前有没有人写过。用python写了个根据关键字找文件的工具,做成了GUI界面。
对于只记得文件部分内容,但找不到文件在哪里的很有帮助。
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))
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))
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):# 行数
for j in range(sheet.shape):# 列数
if key in str(sheet.iat):# 比较单元格中的值是否等于key
row_num = i + 1# Excel的行号是从1开始的
col_num = j + 1# Excel的列号也是从1开始的
index_list.append()
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()
如果是当工具,推荐直接用everything 不太好用,估计文件多了,出结果后直接卡死了 开创者 发表于 2024-8-14 10:23
不太好用,估计文件多了,出结果后直接卡死了
尽量选择大概的文件夹,不要直接选根目录 我来测试一下 window自带这样的功能呀{:301_997:}
谢谢楼主分享,可以作为学习资料
不知道具体使用情况咋样,但愿好用。 进步空间很广阔! 建议还是直接用主流成熟的软件,毕竟现在这种很多很稳定。
页:
[1]