The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

GUI for File Organizer Script

A Graphical User Interface (GUI) for a File Organizer Script provides users with an intuitive and interactive way to sort and manage files in directories based on file types, dates, names, or other criteria. Below is a complete Python script using Tkinter for the GUI and shutil for moving files. This version offers functionality such as selecting a folder, previewing organization structure, and executing the organization.


python
import os import shutil import tkinter as tk from tkinter import filedialog, messagebox, ttk # Dictionary of file types and their extensions FILE_TYPES = { 'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'], 'Documents': ['.pdf', '.doc', '.docx', '.txt', '.xls', '.xlsx', '.ppt', '.pptx'], 'Videos': ['.mp4', '.mov', '.avi', '.mkv'], 'Music': ['.mp3', '.wav', '.aac'], 'Archives': ['.zip', '.rar', '.tar', '.gz'], 'Scripts': ['.py', '.js', '.sh', '.bat'], 'Others': [] } class FileOrganizerGUI: def __init__(self, root): self.root = root self.root.title("File Organizer") self.root.geometry("500x300") self.directory = tk.StringVar() self.create_widgets() def create_widgets(self): frame = ttk.Frame(self.root, padding="20") frame.pack(fill=tk.BOTH, expand=True) ttk.Label(frame, text="Select Folder to Organize:").pack(anchor=tk.W) path_frame = ttk.Frame(frame) path_frame.pack(fill=tk.X, pady=5) path_entry = ttk.Entry(path_frame, textvariable=self.directory, width=50) path_entry.pack(side=tk.LEFT, fill=tk.X, expand=True) browse_button = ttk.Button(path_frame, text="Browse", command=self.browse_directory) browse_button.pack(side=tk.RIGHT) preview_button = ttk.Button(frame, text="Preview File Count by Category", command=self.preview_files) preview_button.pack(fill=tk.X, pady=(10, 5)) self.preview_box = tk.Text(frame, height=8, state=tk.DISABLED) self.preview_box.pack(fill=tk.BOTH, pady=5) organize_button = ttk.Button(frame, text="Organize Files", command=self.organize_files) organize_button.pack(fill=tk.X, pady=(5, 0)) def browse_directory(self): path = filedialog.askdirectory() if path: self.directory.set(path) def preview_files(self): folder = self.directory.get() if not folder or not os.path.isdir(folder): messagebox.showerror("Invalid Folder", "Please select a valid folder.") return file_count = {key: 0 for key in FILE_TYPES} file_count['Others'] = 0 for filename in os.listdir(folder): file_path = os.path.join(folder, filename) if os.path.isfile(file_path): ext = os.path.splitext(filename)[1].lower() found = False for category, extensions in FILE_TYPES.items(): if ext in extensions: file_count[category] += 1 found = True break if not found: file_count['Others'] += 1 self.preview_box.config(state=tk.NORMAL) self.preview_box.delete('1.0', tk.END) for category, count in file_count.items(): self.preview_box.insert(tk.END, f"{category}: {count} file(s)n") self.preview_box.config(state=tk.DISABLED) def organize_files(self): folder = self.directory.get() if not folder or not os.path.isdir(folder): messagebox.showerror("Invalid Folder", "Please select a valid folder.") return for filename in os.listdir(folder): file_path = os.path.join(folder, filename) if os.path.isfile(file_path): ext = os.path.splitext(filename)[1].lower() moved = False for category, extensions in FILE_TYPES.items(): if ext in extensions: self.move_file(file_path, os.path.join(folder, category)) moved = True break if not moved: self.move_file(file_path, os.path.join(folder, 'Others')) messagebox.showinfo("Success", "Files have been organized successfully.") self.preview_files() def move_file(self, src_path, dest_folder): os.makedirs(dest_folder, exist_ok=True) try: shutil.move(src_path, dest_folder) except Exception as e: print(f"Error moving {src_path}: {e}") if __name__ == "__main__": root = tk.Tk() app = FileOrganizerGUI(root) root.mainloop()

Features:

  • Category-based file sorting (Images, Documents, Videos, etc.).

  • Folder selection with a file dialog.

  • Preview window showing file count by category before organizing.

  • Automatic creation of subdirectories if they do not exist.

  • Real-time feedback and error handling.

How to Use:

  1. Run the script using Python 3 (python scriptname.py).

  2. Select a folder using the “Browse” button.

  3. Click “Preview File Count by Category” to see what will be organized.

  4. Click “Organize Files” to move files into categorized folders.

This GUI script streamlines file management tasks, especially for users handling large volumes of disorganized files.

Share this Page your favorite way: Click any app below to share.

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

We respect your email privacy

Categories We Write About