DEV Community

Mate Technologies
Mate Technologies

Posted on

Build a Simple File Explorer in Python with Tkinter – FileMate 🗂️

If you’ve ever wanted to create your own lightweight file explorer using Python, FileMate is a perfect starting point. With Tkinter for GUI, sv_ttk for modern themes, and basic file operations like opening, creating, and deleting files/folders, you can get a fully functional file manager running in minutes.

Here’s a step-by-step guide, complete with code.

Features of FileMate

Navigate folders and view files

Open files and folders

Create new folders

Delete files/folders

Toggle between light and dark mode

Status bar to show current actions

Full Python Script: FileMat_v1.py

import sys
import os
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import sv_ttk

def resource_path(file_name):
    """Get the absolute path to a resource, works for PyInstaller."""
    base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, file_name)

# =========================
# App Setup
# =========================
root = tk.Tk()
root.title("FileMate")
root.geometry("1000x680")
root.minsize(1000, 680)
# root.iconbitmap(resource_path("logo.ico"))
sv_ttk.set_theme("light")

# =========================
# Globals
# =========================
dark_mode_var = tk.BooleanVar(value=False)
current_path_var = tk.StringVar(value=os.path.expanduser("~"))
status_var = tk.StringVar(value="Ready")

# =========================
# Helpers
# =========================
def set_status(msg):
    status_var.set(msg)
    root.update_idletasks()

def toggle_theme():
    sv_ttk.set_theme("dark" if dark_mode_var.get() else "light")
    set_status(f"Theme switched to {'Dark' if dark_mode_var.get() else 'Light'} mode")

def refresh_file_list(path=None):
    path = path or current_path_var.get()
    if not os.path.exists(path):
        messagebox.showerror("Error", "Path does not exist!")
        return
    current_path_var.set(path)
    file_listbox.delete(0, tk.END)
    try:
        for item in os.listdir(path):
            full_path = os.path.join(path, item)
            display = f"[DIR] {item}" if os.path.isdir(full_path) else f"[FILE] {item}"
            file_listbox.insert(tk.END, display)
        set_status(f"Listing files in {path}")
    except PermissionError:
        messagebox.showerror("Error", "Permission denied.")

def open_selected():
    selection = file_listbox.curselection()
    if not selection:
        return
    item_text = file_listbox.get(selection[0])
    item_name = item_text.split(" ", 1)[1]
    path = os.path.join(current_path_var.get(), item_name)
    if os.path.isdir(path):
        refresh_file_list(path)
    else:
        try:
            os.startfile(path)
            set_status(f"Opened {item_name}")
        except Exception as e:
            messagebox.showerror("Error", f"Cannot open file: {e}")

def go_up():
    parent = os.path.dirname(current_path_var.get())
    refresh_file_list(parent)

def create_folder():
    folder_name = simple_input("Create Folder", "Enter folder name:")
    if folder_name:
        path = os.path.join(current_path_var.get(), folder_name)
        try:
            os.makedirs(path)
            refresh_file_list()
            set_status(f"Folder '{folder_name}' created")
        except FileExistsError:
            messagebox.showerror("Error", "Folder already exists.")
        except Exception as e:
            messagebox.showerror("Error", str(e))

def delete_item():
    selection = file_listbox.curselection()
    if not selection:
        return
    item_text = file_listbox.get(selection[0])
    item_name = item_text.split(" ", 1)[1]
    path = os.path.join(current_path_var.get(), item_name)
    if messagebox.askyesno("Confirm Delete", f"Are you sure you want to delete '{item_name}'?"):
        try:
            if os.path.isdir(path):
                os.rmdir(path)
            else:
                os.remove(path)
            refresh_file_list()
            set_status(f"Deleted '{item_name}'")
        except Exception as e:
            messagebox.showerror("Error", str(e))

def simple_input(title, prompt):
    win = tk.Toplevel(root)
    win.title(title)
    win.geometry("300x120")
    win.resizable(False, False)
    result = tk.StringVar()

    ttk.Label(win, text=prompt).pack(pady=(10, 5))
    entry = ttk.Entry(win, textvariable=result, width=30)
    entry.pack(pady=(0, 10))
    entry.focus_set()

    def submit():
        win.destroy()

    ttk.Button(win, text="OK", command=submit).pack()
    win.grab_set()
    root.wait_window(win)
    return result.get()

# =========================
# Status Bar
# =========================
ttk.Label(
    root,
    textvariable=status_var,
    anchor="w",
    font=("Segoe UI", 10)
).pack(side=tk.BOTTOM, fill="x")

# =========================
# Main Frame
# =========================
main_frame = ttk.Frame(root, padding=20)
main_frame.pack(expand=True, fill="both")

# Toolbar
toolbar = ttk.Frame(main_frame)
toolbar.pack(fill="x", pady=(0, 10))

ttk.Button(toolbar, text="Up", command=go_up).pack(side="left", padx=5)
ttk.Button(toolbar, text="New Folder", command=create_folder).pack(side="left", padx=5)
ttk.Button(toolbar, text="Delete", command=delete_item).pack(side="left", padx=5)
ttk.Checkbutton(toolbar, text="Dark Mode", variable=dark_mode_var, command=toggle_theme).pack(side="right", padx=5)

# Current Path
path_frame = ttk.Frame(main_frame)
path_frame.pack(fill="x", pady=(0, 10))
ttk.Label(path_frame, text="Current Path:").pack(side="left")
ttk.Entry(path_frame, textvariable=current_path_var, width=80).pack(side="left", padx=(5, 0))
ttk.Button(path_frame, text="Go", command=lambda: refresh_file_list(current_path_var.get())).pack(side="left", padx=5)

# File List
file_frame = ttk.Frame(main_frame)
file_frame.pack(expand=True, fill="both")
file_listbox = tk.Listbox(file_frame, font=("Segoe UI", 11))
file_listbox.pack(side="left", expand=True, fill="both")
file_listbox.bind("<Double-Button-1>", lambda e: open_selected())

scrollbar = ttk.Scrollbar(file_frame, orient="vertical", command=file_listbox.yview)
scrollbar.pack(side="right", fill="y")
file_listbox.config(yscrollcommand=scrollbar.set)

# Initialize file list
refresh_file_list()

# =========================
# Run App
# =========================
root.mainloop()
Enter fullscreen mode Exit fullscreen mode

How it Works

Tkinter GUI – The GUI uses ttk widgets for modern styling and sv_ttk for theme switching.

File Navigation – The app dynamically lists files and folders in a directory.

File Operations – Double-click to open files, create new folders, and delete items.

Dark Mode – Toggle between light and dark themes with a single checkbox.

Status Bar – Shows real-time messages for all actions.

Screenshot Preview

Insert a screenshot of your running FileMate here

Installation
pip install sv_ttk

Then simply run:

python FileMat_v1.py

Closing Thoughts

FileMate is a lightweight, Python-based file manager. It’s an excellent project for beginners to explore file I/O, Tkinter GUIs, and theming. You can extend it with features like drag-and-drop, file search, or multi-selection operations.

Top comments (0)