A simple yet powerful Python script to automatically organize files in a directory by moving them into subfolders based on their file extension.
This helps keep your folders β like Downloads, Documents, or Desktop β clean and tidy.
The project includes a full suite of unit tests to ensure the script's logic is reliable and bug-free.
- π§ Automatic Sorting: Sorts files into categories like Images, Documents, Videos, etc.
- βοΈ Customizable: Easily add new file extensions or change folder names by editing the
file_extentions_mapdictionary. - π‘οΈ Safe: Skips directories and only moves files.
- πͺ Intelligent: Creates destination folders if they don't already exist.
- π‘ Lightweight: Uses only Pythonβs standard libraries β no external packages needed.
- β
Well-Tested: Comes with
unittestsupport to verify all functions work correctly.
Make sure you have Python 3.x installed.
You can verify your Python version with:
python --version
- Save the main script file as organizer.py.
- Open your terminal or command prompt.
- Navigate to the folder containing organizer.py.
- Run the script by passing the path of the folder you want to organize as a command-line argument.
python organizer.py /path/to/your/folder
To organize your Downloads folder:
bash Copy code python organizer.py "C:/Users/YourName/Downloads"
Downloads/
βββ vacation.jpg
βββ quarterly-report.docx
βββ project.zip
βββ resume.pdf
βββ funny-cat.mp4
βββ install_notes.txt
Downloads/
βββ Archives/
β βββ project.zip
βββ Documents/
β βββ quarterly-report.docx
β βββ resume.pdf
β βββ install_notes.txt
βββ Images/
β βββ vacation.jpg
βββ Videos/
βββ funny-cat.mp4
The logic for sorting files is handled by the file_extentions_map dictionary inside organizer.py. You can modify it to add or change categories easily.
file_extentions_map = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".svg", ".webp"],
"Documents": [".pdf", ".docx", ".txt", ".pptx", ".xlsx"],
"Videos": [".mp4", ".mkv", ".mov", ".avi"],
"Archives": [".zip", ".rar", ".7z", ".tar"],
}
Add your own extensions or categories as needed β the script will automatically create those folders and sort files accordingly.
β Running the Tests A test suite is included to ensure the sorting logic works correctly. This is especially useful if youβve customized the script.
Save the test file as test_organizer.py in the same directory as organizer.py.
Run the tests with:
python test_organizer.py
Ran 1 test in 0.001s
OK If all tests pass β your organizer is working perfectly β
π File-Organizer/
βββ organizer.py
βββ test_organizer.py
βββ README.md
Run this script on folders like Downloads, Desktop, or Projects for instant cleanup. You can schedule it using Windows Task Scheduler or macOS Automator to run periodically. Works seamlessly across Windows, macOS, and Linux.