DEV Community

Mate Technologies
Mate Technologies

Posted on

๐Ÿ” Build a Desktop File Content Search App with Python & Tkinter

Searching filenames is easy โ€” searching inside files is where things get interesting.

In this tutorial, weโ€™ll build a desktop File Search System using Python, Tkinter, and ttkbootstrap that can:

Recursively scan folders

Search inside files (TXT, CSV, XLSX, PDF, XML)

Support regex searches

Show live progress, speed & ETA

Preview matched content

Export results to CSV

๐Ÿ“ฆ Download / Clone from GitHub
๐Ÿ‘‰ https://github.com/rogers-cyber/File-Search-System/releases

๐Ÿงฐ Step 1: Install Required Libraries

Make sure you have Python 3.9+ installed, then run:

pip install ttkbootstrap xlrd PyPDF2
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Step 2: Import Required Modules

Weโ€™ll import modules for:

file handling

regex searching

threading

GUI

file parsing

import os
import sys
import re
import fnmatch
import threading
import time
import xlrd
import PyPDF2
import xml.etree.ElementTree as ET
import tkinter as tk
from tkinter import filedialog, messagebox
import ttkbootstrap as tb
from ttkbootstrap.constants import *
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Step 3: App Configuration

Define your app name and version in one place.

APP_NAME = "File Search System"
APP_VERSION = "1.0.1"
Enter fullscreen mode Exit fullscreen mode

๐ŸชŸ Step 4: Initialize the Application Window

Create the main Tkinter window and apply a modern theme.

app = tk.Tk()
style = tb.Style(theme="superhero")
app.title(f"{APP_NAME} {APP_VERSION}")
app.geometry("1230x680")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ–ผ Step 5: Handle App Resources (Icons)

This ensures compatibility with PyInstaller builds.

def resource_path(file_name):
    base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, file_name)
Enter fullscreen mode Exit fullscreen mode

โ„น๏ธ Step 6: Create an โ€œAboutโ€ Dialog

This adds a professional touch to your app.

def show_about():
    messagebox.showinfo(
        f"About {APP_NAME} v{APP_VERSION}",
        f"{APP_NAME} v{APP_VERSION}\n"
        "Production Edition\n\n"
        "A desktop utility for searching inside files.\n\n"
        "โœ” Regex search\n"
        "โœ” Multi-file support\n"
        "โœ” Live progress & previews\n\n"
        "ยฉ 2026 Mate Technologies"
    )
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“‹ Step 7: Add a Menu Bar

Attach the About dialog to a Help menu.

menubar = tk.Menu(app)
help_menu = tk.Menu(menubar, tearoff=0)
help_menu.add_command(label="About", command=show_about)
menubar.add_cascade(label="Help", menu=help_menu)
app.config(menu=menubar)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“‚ Step 8: Build the Search Settings UI

This lets users choose a folder and search term.

frame1 = tb.Labelframe(app, text="Search Settings", padding=10)
frame1.pack(fill="x", padx=10, pady=6)

root_path = tk.StringVar()
search_var = tk.StringVar()
regex_var = tk.BooleanVar()

tb.Label(frame1, text="Root Folder:").pack(side="left")
tb.Entry(frame1, textvariable=root_path, width=50).pack(side="left")
tb.Button(frame1, text="Browse", command=lambda: root_path.set(
    filedialog.askdirectory())).pack(side="left")
Enter fullscreen mode Exit fullscreen mode

๐Ÿงน Step 9: Add File Filters & Controls

Users can limit file types and exclude folders.

file_filter = tk.StringVar(value="*")
exclude_folders = tk.StringVar(value="node_modules .git __pycache__")

search_btn = tb.Button(frame1, text="๐Ÿ” SEARCH", bootstyle="success")
stop_btn = tb.Button(frame1, text="๐Ÿ›‘ STOP", bootstyle="danger-outline", state="disabled")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Š Step 10: Progress Bar & Live Stats

We track progress, speed, ETA, and matches.

progress_var = tk.IntVar()
tb.Progressbar(app, variable=progress_var, maximum=100).pack(fill="x")

eta_lbl = tb.Label(app, text="ETA: --")
spd_lbl = tb.Label(app, text="Speed: -- files/s")
counter_lbl = tb.Label(app, text="Matches: 0")
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“„ Step 11: Display Search Results

A TreeView shows file paths and preview text.

tree = tb.Treeview(app, columns=("path", "preview"), show="headings")
tree.heading("path", text="File Path")
tree.heading("preview", text="Match Preview")
tree.pack(fill="both", expand=True)
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Step 12: Search Logic Helpers
File filtering

def allowed_file(name):
    pats = file_filter.get().lower().split()
    return "*" in pats or any(fnmatch.fnmatch(name, p) for p in pats)
Enter fullscreen mode Exit fullscreen mode

Folder exclusion

def excluded_dir(path):
    return any(ex in path.lower() for ex in exclude_folders.get().split())
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Step 13: Read File Contents with Preview

This function handles TXT, CSV, XLSX, PDF, and XML.

def read_file_content_with_preview(path, query, use_regex=False):
    if path.endswith(".txt"):
        with open(path, "r", errors="ignore") as f:
            for i, line in enumerate(f, 1):
                if re.search(query, line, re.I):
                    return True, f"Line {i}: {line[:100]}"
    return False, ""
Enter fullscreen mode Exit fullscreen mode

(The full version in GitHub supports all formats.)

๐Ÿš€ Step 14: Run the Search in a Thread

This keeps the UI responsive.

def run_search():
    for root, dirs, files in os.walk(root_path.get()):
        for file in files:
            if allowed_file(file):
                path = os.path.join(root, file)
                found, preview = read_file_content_with_preview(
                    path, search_var.get(), regex_var.get())

search_btn.config(command=lambda:
    threading.Thread(target=run_search, daemon=True).start()
)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ค Step 15: Export Results to CSV

def export_results():
    file = filedialog.asksaveasfilename(defaultextension=".csv")
    with open(file, "w", encoding="utf-8") as f:
        for row in results_list:
            f.write(",".join(row) + "\n")
Enter fullscreen mode Exit fullscreen mode

โ–ถ๏ธ Step 16: Start the App
app.mainloop()

โœ… Final Result

You now have a fully functional desktop file content search tool with:

Clean UI

Threaded scanning

Multiple file format support

Live progress & previews

CSV export

๐Ÿ“ฆ Download / Clone from GitHub
๐Ÿ‘‰ https://github.com/rogers-cyber/File-Search-System/releases

Top comments (0)