Managing a cluttered Downloads folder is a struggle we all face. Images, documents, videos, executables, and random files pile up quickly, making it difficult to find what you need. Thatโs where Python automation comes to the rescue!
In this blog, Iโll walk you through how I built a Python File Organizer Script that automatically sorts files into categorized subfolders like Images, Documents, Videos, Audio, Archives, Config, and more.
๐ Problem Statement
Create a Python script that automatically organizes files in a folder by file type โ putting images, documents, videos, etc., into separate subfolders.
๐ Features of the Script
- โ Automatically detects file types by extension
- โ Creates subfolders if they donโt exist
- โ Moves files into respective folders
- โ Prints logs for confirmation (moved / skipped files)
- โ Easy to customize with new extensions
๐ ๏ธ Supported File Types
-
Images โ
.jpg
,.jpeg
,.png
,.gif
-
Documents โ
.pdf
,.docx
,.txt
,.xlsx
,.pptx
-
Videos โ
.mp4
,.avi
,.mkv
-
Audio โ
.mp3
,.wav
-
Archives โ
.zip
,.rar
-
Scripts โ
.py
,.js
,.md
-
Config โ
.ini
-
Applications โ
.exe
๐ Python Code
Hereโs the complete script:
import os
import shutil
def organize_folder(folder_path):
# Define file type categories
file_types = {
'Images': ['.jpg', '.jpeg', '.png', '.gif'],
'Documents': ['.pdf', '.docx', '.txt', '.xlsx', '.pptx'],
'Videos': ['.mp4', '.avi', '.mkv'],
'Audio': ['.mp3', '.wav'],
'Archives': ['.zip', '.rar'],
'Scripts': ['.py', '.js', '.md'],
'Config': ['.ini'],
'Applications': ['.exe']
}
print(f"๐ Organizing files in: {folder_path}\n")
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)
moved = False
for folder, extensions in file_types.items():
if ext.lower() in extensions:
target_folder = os.path.join(folder_path, folder)
os.makedirs(target_folder, exist_ok=True)
shutil.move(file_path, os.path.join(target_folder, filename))
print(f"โ
Moved: {filename} โ {folder}/")
files_moved += 1
moved = True
break
if not moved:
print(f"โ ๏ธ Skipped (Unknown type): {filename}")
print(f"\nโจ Done! Total files moved: {files_moved}")
# Run the script
organize_folder(r"C:\\Users\\imran\\Downloads")
๐ธ Example Output
๐ Organizing files in: C:\Users\imran\Downloads
โ
Moved: photo.jpg โ Images/
โ
Moved: resume.docx โ Documents/
โ
Moved: movie.mp4 โ Videos/
โ ๏ธ Skipped (Unknown type): setup.tmp
โจ Done! Total files moved: 5
๐ฏ Why This Script is Useful?
- Keeps your Downloads folder neat and tidy
- Saves time searching for files
- Boosts productivity with automation
- Works on Windows, Mac, and Linux
๐ฎ Future Improvements
- Add an Others folder for unknown file types
- Schedule script with Task Scheduler (Windows) or Cron Jobs (Linux/Mac)
- Add a GUI interface for non-technical users
๐ Final Thoughts
This project is a simple yet powerful example of how Python automation can simplify everyday tasks. With just a few lines of code, you can turn a messy folder into a well-organized one.
๐ก If you want to try it out, check out the GitHub repo here ๐ python-file-organizer-automation
โ๏ธ Author: Imran Ahmad
๐ Passionate about Python Automation, Selenium, and scripting for productivity.
Top comments (0)