DEV Community

Cover image for Build a Smart File Organizer in 15 Minutes With Python
Xinglin Ming
Xinglin Ming

Posted on

Build a Smart File Organizer in 15 Minutes With Python

Build a Smart File Organizer in 15 Minutes With Python

Ever stared at your Downloads folder with 500+ files and felt overwhelmed? I built this Python script to solve that problem permanently.

The Problem

We all have that one folder where files go to die. Over months, it accumulates PDFs, images, and code snippets - everything mixed together.

The Solution: Smart File Organizer

Here's a complete, production-ready file organizer that I use daily:

import os, shutil
from pathlib import Path
from datetime import datetime

class SmartFileOrganizer:
    def __init__(self, base_path):
        self.base_path = Path(base_path)
        self.stats = {'moved': 0, 'errors': 0}

    def organize_by_type(self):
        type_map = {
            'Documents': ['.pdf', '.docx', '.txt', '.xlsx'],
            'Images': ['.jpg', '.png', '.gif', '.bmp', '.svg'],
            'Archives': ['.zip', '.rar', '.7z', '.tar'],
            'Code': ['.py', '.js', '.html', '.css', '.java'],
            'Videos': ['.mp4', '.avi', '.mkv', '.mov'],
        }

        for folder_name, extensions in type_map.items():
            folder_path = self.base_path / folder_name
            folder_path.mkdir(exist_ok=True)
            for ext in extensions:
                for f in self.base_path.glob(f'*{ext}'):
                    if f.is_file():
                        shutil.move(str(f), str(folder_path / f.name))
                        self.stats['moved'] += 1
        return self.stats

organizer = SmartFileOrganizer('C:/Users/YourName/Downloads')
r = organizer.organize_by_type()
print(f'Done! Moved {r["moved"]} files')
Enter fullscreen mode Exit fullscreen mode

Advanced Features

  • Duplicate detection: Find and remove duplicate files using MD5 hashing
  • Auto-cleanup: Remove temp files older than 30 days
  • Watch mode: Monitor a folder and auto-organize new files
def find_duplicates(folder):
    import hashlib
    hash_map = {}
    for f in Path(folder).rglob('*'):
        if f.is_file():
            h = hashlib.md5(f.read_bytes()).hexdigest()
            if h in hash_map:
                print(f'Duplicate: {f} == {hash_map[h]}')
            else:
                hash_map[h] = f
Enter fullscreen mode Exit fullscreen mode

Why Use This?

  • Save hours of manual file sorting every week
  • Prevent data loss by keeping files organized
  • Runs on schedule, no manual effort needed

Follow me for daily Python tips and production-ready tools!

Top comments (0)