file organizer in python using GUI

introduction
In today’s digital world, our devices accumulate a wide variety of files—documents, images, videos, music, code files, and archives. Over time, managing these files in a cluttered folder (such as the Downloads or Desktop folder) becomes overwhelming and inefficient. Manually organizing them into appropriate subfolders can be tedious and time-consuming.
To solve this problem, we’ve developed a File Organizer in python using gui Tool with a Graphical User Interface (GUI) using Python’s built-in Tkinter
library. This tool helps automate the file organization process by scanning a selected folder and categorizing files into subfolders based on their file extensions. For example:
Images like
.jpg
,.png
go into an “Images” folderDocuments like
.pdf
,.docx
go into a “Documents” folderVideos, music, scripts, and archives are similarly sorted
Unlike command-line scripts, this tool is user-friendly, click-based, and doesn’t require any programming knowledge to use. Users simply click a “Browse Folder” button to choose the directory they want to organize and then click “Start Organizing” to perform the cleanup. All files are neatly moved into their respective folders automatically.
This project is especially useful for:
Students and teachers managing downloaded materials
Office workers organizing client files or media
Developers who want to clean project folders
Anyone with a messy Downloads folder
The project leverages:
os
module for file system operationsshutil
for moving filesTkinter
for GUI components like buttons, labels, and dialogs
This File Organizer project is a great example of how Python can be used to build practical utilities that improve productivity and make everyday computer tasks more efficient and enjoyable.
steps to create file organizer in python using gui
Step 1: Import Required Modules
Begin by importing the necessary Python libraries for GUI, file handling, and user dialogs.
import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
os
: To interact with the file systemshutil
: To move filestkinter
: To create GUI windows and buttonsfiledialog
: To allow the user to browse foldersmessagebox
: To show error messages
Step 2: Define File Categories
Create a dictionary that maps folder names (like “Images”, “Documents”) to their associated file extensions.
file_types = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"Documents": [".pdf", ".docx", ".doc", ".txt", ".xlsx", ".pptx"],
"Videos": [".mp4", ".mov", ".avi", ".mkv"],
"Audio": [".mp3", ".wav", ".aac"],
"Archives": [".zip", ".rar", ".7z", ".tar", ".gz"],
"Scripts": [".py", ".js", ".html", ".css", ".java", ".cpp"],
"Others": []
}
This dictionary is used to decide where each file should be moved based on its extension.
Step 3: Create the File Organizer Function
Define a function organize_files()
that:
Gets the selected folder path
Scans all files in that folder
Determines each file’s type by extension
Moves the file to its corresponding subfolder
def organize_files():
folder_path = path_var.get()
...
Step 4: Create the Folder Selection Function
Add a function browse_folder()
that allows the user to choose a folder from a dialog box. The selected folder path is stored in a Tkinter StringVar
.
def browse_folder():
folder = filedialog.askdirectory()
if folder:
path_var.set(folder)
Step 5: Build the GUI Layout
Now create the actual GUI using Tkinter:
A title label
An entry box to display selected folder path
A “Browse Folder” button
A “Start Organizing” button
A label to display the result
root = tk.Tk()
root.title("File Organizer")
root.geometry("500x250")
root.config(bg="white")
path_var = tk.StringVar()
Add GUI components using .pack()
:
title = tk.Label(root, text="File Organizer Tool", font=("Arial", 16), bg="white")
title.pack(pady=10)
path_entry = tk.Entry(root, textvariable=path_var, width=50)
path_entry.pack(pady=5)
browse_btn = tk.Button(root, text="Browse Folder", command=browse_folder, ...)
browse_btn.pack()
organize_btn = tk.Button(root, text="Start Organizing", command=organize_files, ...)
organize_btn.pack(pady=10)
result_label = tk.Label(root, text="", ...)
result_label.pack(pady=10)
Step 6: Launch the GUI Application
Finally, start the main loop to open the GUI window and listen for events.
root.mainloop()
code explanation
1. Import Required Libraries
import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
os
: Interact with the file system (check folders, list files).shutil
: Move files between directories.tkinter
: Build GUI elements.filedialog
: Browse and select folders.messagebox
: Show pop-up alerts or error messages.
2. Define File Type Categories
file_types = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"Documents": [".pdf", ".docx", ".doc", ".txt", ".xlsx", ".pptx"],
"Videos": [".mp4", ".mov", ".avi", ".mkv"],
"Audio": [".mp3", ".wav", ".aac"],
"Archives": [".zip", ".rar", ".7z", ".tar", ".gz"],
"Scripts": [".py", ".js", ".html", ".css", ".java", ".cpp"],
"Others": []
}
This dictionary tells the script how to categorize files based on their extensions.
If a file extension doesn’t match any listed category, it goes into the “Others” folder.
3. Define organize_files()
Function
def organize_files():
folder_path = path_var.get()
Gets the user-selected folder path from the entry box.
if not os.path.exists(folder_path):
messagebox.showerror("Error", "Please select a valid folder.")
return
Checks if the folder exists. If not, shows an error and stops the function.
files_moved = 0
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
Loops through each file in the folder and builds the full path.
if os.path.isfile(file_path):
_, ext = os.path.splitext(filename)
ext = ext.lower()
Filters only files (not folders) and gets the file extension in lowercase.
found = False
for category, extensions in file_types.items():
if ext in extensions:
target_folder = os.path.join(folder_path, category)
found = True
break
Searches for the category matching the file’s extension and sets the target folder accordingly.
if not found:
target_folder = os.path.join(folder_path, "Others")
If the extension is unknown, move it to “Others”.
os.makedirs(target_folder, exist_ok=True)
shutil.move(file_path, os.path.join(target_folder, filename))
files_moved += 1
Creates the target folder if it doesn’t exist, then moves the file there and increments the file count.
result_label.config(text=f"Organized {files_moved} files successfully!")
Displays a message in the GUI showing how many files were moved.
4. Define browse_folder()
Function
def browse_folder():
folder = filedialog.askdirectory()
if folder:
path_var.set(folder)
Opens a folder selection dialog.
If a folder is selected, its path is stored in a
StringVar
for display in the entry box.
5. Set Up the GUI
root = tk.Tk()
root.title("File Organizer")
root.geometry("500x250")
root.config(bg="white")
Creates the main window, sets the title and size, and changes background to white.
path_var = tk.StringVar()
A variable to dynamically update the entry box with the selected path.
6. Add GUI Components
title = tk.Label(root, text="File Organizer Tool", font=("Arial", 16), bg="white")
title.pack(pady=10)
Displays the heading/title.
path_entry = tk.Entry(root, textvariable=path_var, width=50)
path_entry.pack(pady=5)
Entry box to show the selected folder path.
browse_btn = tk.Button(root, text="Browse Folder", command=browse_folder, bg="#2196F3", fg="white", padx=10, pady=5)
browse_btn.pack()
Button to browse and select the folder.
organize_btn = tk.Button(root, text="Start Organizing", command=organize_files, bg="#4CAF50", fg="white", padx=10, pady=5)
organize_btn.pack(pady=10)
Button to start organizing files in the selected folder.
result_label = tk.Label(root, text="", font=("Arial", 12), fg="green", bg="white")
result_label.pack(pady=10)
Displays the final result: how many files were organized.
7. Run the GUI
root.mainloop()
Starts the Tkinter event loop to keep the window open and respond to button clicks.
Summary
Part | Purpose |
---|---|
file_types | Categorizes files by extensions |
organize_files() | Organizes files based on extension |
browse_folder() | Opens a folder picker dialog |
Tkinter Widgets | Entry, Buttons, and Labels for interactive GUI |
shutil.move() | Moves files to new categorized folders |
source code
import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
# File type mapping
file_types = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"Documents": [".pdf", ".docx", ".doc", ".txt", ".xlsx", ".pptx"],
"Videos": [".mp4", ".mov", ".avi", ".mkv"],
"Audio": [".mp3", ".wav", ".aac"],
"Archives": [".zip", ".rar", ".7z", ".tar", ".gz"],
"Scripts": [".py", ".js", ".html", ".css", ".java", ".cpp"],
"Others": []
}
# Organize files by type
def organize_files():
folder_path = path_var.get()
if not os.path.exists(folder_path):
messagebox.showerror("Error", "Please select a valid folder.")
return
files_moved = 0
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
_, ext = os.path.splitext(filename)
ext = ext.lower()
found = False
for category, extensions in file_types.items():
if ext in extensions:
target_folder = os.path.join(folder_path, category)
found = True
break
if not found:
target_folder = os.path.join(folder_path, "Others")
os.makedirs(target_folder, exist_ok=True)
shutil.move(file_path, os.path.join(target_folder, filename))
files_moved += 1
result_label.config(text=f"Organized {files_moved} files successfully!")
# Browse for directory
def browse_folder():
folder = filedialog.askdirectory()
if folder:
path_var.set(folder)
# GUI Setup
root = tk.Tk()
root.title("File Organizer")
root.geometry("500x250")
root.config(bg="white")
path_var = tk.StringVar()
title = tk.Label(root, text="File Organizer Tool", font=("Arial", 16), bg="white")
title.pack(pady=10)
path_entry = tk.Entry(root, textvariable=path_var, width=50)
path_entry.pack(pady=5)
browse_btn = tk.Button(root, text="Browse Folder", command=browse_folder, bg="#2196F3", fg="white", padx=10, pady=5)
browse_btn.pack()
organize_btn = tk.Button(root, text="Start Organizing", command=organize_files, bg="#4CAF50", fg="white", padx=10, pady=5)
organize_btn.pack(pady=10)
result_label = tk.Label(root, text="", font=("Arial", 12), fg="green", bg="white")
result_label.pack(pady=10)
root.mainloop()
Add Your Heading Text Here

