A Python script that automatically sorts any messy folder into subfolders by file type — Images, Documents, Videos, Audio, and more. Run it once and your Downloads folder is clean.
Create a dictionary mapping folder names to the file extensions that belong in them.
# organizer.py
FILE_TYPES = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp', '.ico'],
'Documents': ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.csv'],
'Videos': ['.mp4', '.mov', '.avi', '.mkv', '.wmv', '.flv', '.webm'],
'Audio': ['.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a'],
'Archives': ['.zip', '.rar', '.7z', '.tar', '.gz'],
'Code': ['.py', '.js', '.html', '.css', '.java', '.cpp', '.c', '.json', '.xml'],
'Executables':['.exe', '.msi', '.dmg', '.pkg', '.deb'],
}
Loop through every file in the target folder and move it to the right subfolder.
import os
import shutil
from pathlib import Path
FILE_TYPES = {
'Images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg', '.webp', '.ico'],
'Documents': ['.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.csv'],
'Videos': ['.mp4', '.mov', '.avi', '.mkv', '.wmv', '.flv', '.webm'],
'Audio': ['.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a'],
'Archives': ['.zip', '.rar', '.7z', '.tar', '.gz'],
'Code': ['.py', '.js', '.html', '.css', '.java', '.cpp', '.c', '.json', '.xml'],
'Executables':['.exe', '.msi', '.dmg', '.pkg', '.deb'],
}
def get_category(extension):
for category, extensions in FILE_TYPES.items():
if extension.lower() in extensions:
return category
return 'Other'
def organize_folder(folder_path):
folder = Path(folder_path)
moved = 0
for file in folder.iterdir():
if not file.is_file():
continue # skip subfolders
category = get_category(file.suffix)
dest_folder = folder / category
dest_folder.mkdir(exist_ok=True)
dest = dest_folder / file.name
# Avoid overwriting — rename if file already exists
counter = 1
while dest.exists():
dest = dest_folder / f"{file.stem}_{counter}{file.suffix}"
counter += 1
shutil.move(str(file), str(dest))
print(f"Moved: {file.name} → {category}/")
moved += 1
print(f"\nDone! {moved} files organised.")
organize_folder(r"C:\Users\YourName\Downloads") # change to your folder
Moved: photo.jpg → Images/ Moved: report.pdf → Documents/ Moved: song.mp3 → Audio/ Moved: installer.exe → Executables/ Moved: notes.txt → Documents/ Moved: archive.zip → Archives/ Done! 6 files organised.
Accept the target folder as a command-line argument so you can reuse the script on any folder.
import sys
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python organizer.py <folder_path>")
print("Example: python organizer.py C:/Users/John/Downloads")
sys.exit(1)
target = sys.argv[1]
if not os.path.isdir(target):
print(f"Error: '{target}' is not a valid folder")
sys.exit(1)
organize_folder(target)
Preview what will be moved without actually moving anything — useful before running on an important folder.
def organize_folder(folder_path, dry_run=False):
folder = Path(folder_path)
moved = 0
for file in folder.iterdir():
if not file.is_file():
continue
category = get_category(file.suffix)
dest_folder = folder / category
action = "Would move" if dry_run else "Moved"
if not dry_run:
dest_folder.mkdir(exist_ok=True)
dest = dest_folder / file.name
counter = 1
while dest.exists():
dest = dest_folder / f"{file.stem}_{counter}{file.suffix}"
counter += 1
shutil.move(str(file), str(dest))
print(f"{action}: {file.name} → {category}/")
moved += 1
mode = "DRY RUN — nothing moved" if dry_run else "Done!"
print(f"\n{mode} {moved} files.")
# Preview first
organize_folder("Downloads", dry_run=True)
Your file organizer runs entirely on Python's standard library — no pip install, no dependencies that can break. Schedule it to run automatically and you'll never have a messy Downloads folder again.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.