Categories We Write About

Create Python-based UI for file sorting

Creating a Python-based UI for file sorting can be done efficiently using Tkinter for the interface and Python’s built-in modules for file operations. Below is a complete example of a simple file sorting application that lets you:

  • Select a source folder

  • Select a destination folder

  • Sort files by their extensions into folders inside the destination

python
import os import shutil from tkinter import Tk, Label, Button, Entry, filedialog, messagebox, StringVar class FileSorterApp: def __init__(self, master): self.master = master master.title("File Sorting Tool") self.source_path = StringVar() self.dest_path = StringVar() # Source folder selection Label(master, text="Source Folder:").grid(row=0, column=0, sticky="e", padx=5, pady=5) self.source_entry = Entry(master, width=50, textvariable=self.source_path) self.source_entry.grid(row=0, column=1, padx=5) Button(master, text="Browse", command=self.browse_source).grid(row=0, column=2, padx=5) # Destination folder selection Label(master, text="Destination Folder:").grid(row=1, column=0, sticky="e", padx=5, pady=5) self.dest_entry = Entry(master, width=50, textvariable=self.dest_path) self.dest_entry.grid(row=1, column=1, padx=5) Button(master, text="Browse", command=self.browse_destination).grid(row=1, column=2, padx=5) # Sort button Button(master, text="Sort Files", command=self.sort_files, width=20).grid(row=2, column=1, pady=20) def browse_source(self): folder_selected = filedialog.askdirectory() if folder_selected: self.source_path.set(folder_selected) def browse_destination(self): folder_selected = filedialog.askdirectory() if folder_selected: self.dest_path.set(folder_selected) def sort_files(self): source = self.source_path.get() dest = self.dest_path.get() if not source or not os.path.isdir(source): messagebox.showerror("Error", "Please select a valid source folder.") return if not dest or not os.path.isdir(dest): messagebox.showerror("Error", "Please select a valid destination folder.") return try: files = [f for f in os.listdir(source) if os.path.isfile(os.path.join(source, f))] if not files: messagebox.showinfo("Info", "No files found in the source folder.") return for file in files: ext = os.path.splitext(file)[1].lower().strip('.') if not ext: ext = "no_extension" target_folder = os.path.join(dest, ext) if not os.path.exists(target_folder): os.makedirs(target_folder) shutil.move(os.path.join(source, file), os.path.join(target_folder, file)) messagebox.showinfo("Success", "Files sorted successfully!") except Exception as e: messagebox.showerror("Error", f"An error occurred: {e}") if __name__ == "__main__": root = Tk() root.geometry("700x150") app = FileSorterApp(root) root.mainloop()

How this works:

  • You pick a source folder containing files to sort.

  • You pick a destination folder where sorted folders will be created.

  • The program moves each file into a folder named after its file extension (e.g., .jpg files go into a jpg folder).

  • Files without an extension go into a no_extension folder.

  • It uses basic Tkinter UI elements: Labels, Entry fields, Buttons, and file dialogs.

  • It shows error and success messages with message boxes.

If you want, I can help you extend this with sorting by date, size, or other criteria.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About