📂 Build a File Size Organizer GUI in Python with Tkinter
Source: Dev.to
Overview
In this tutorial we’ll build a Python application that:
- Lets users monitor folders.
- Categorises files by size.
- Organises files into folders automatically.
- Provides a live file preview with filtering and sorting.
We’ll use Tkinter, watchdog, threading, and tkinterdnd2 for drag‑and‑drop support.
Step 1 – Import Required Libraries
First, import the libraries for the GUI, file handling, and background monitoring.
import os
import json
import shutil
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from tkinterdnd2 import DND_FILES, TkinterDnD
from threading import Thread
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import sv_ttk
Explanation
| Library | Purpose |
|---|---|
os, shutil | Interact with the file system |
json | Save/load folder configurations |
tkinter, ttk | GUI elements |
tkinterdnd2 | Drag‑and‑drop functionality |
watchdog | Real‑time folder monitoring |
threading | Run long‑running tasks without freezing the GUI |
Step 2 – Configuration & Helper Functions
We’ll store monitored folders in a JSON file and create a few helper functions.
CONFIG_FILE = "folders_data.json"
def load_folders():
"""Read previously monitored folders from JSON."""
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
return json.load(f)
return []
def save_folders(folders_data):
"""Write current folders to JSON for persistence."""
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(folders_data, f, ensure_ascii=False, indent=4)
Utility Functions for File Handling
def sanitize_folder_name(name):
"""Remove illegal characters for folder names."""
return "".join(c for c in name if c not in r'<>:"/\\|?*')
def format_size(size_bytes):
"""Convert bytes into a human‑readable string."""
if size_bytes >', drop)
Step 12 – GUI Layout
Set up the main window with frames, buttons, the Treeview, filter dropdowns, a progress bar, and a status bar.
Because the layout code is extensive, break it into logical sub‑steps (e.g., create frames, add controls, configure grid/pack, bind events) in your final tutorial.
