T

TechIdea

Ecosystem

Back to python Projects
Beginner Level

Python Desktop File Organizer Automation

Write a Python script that automatically scans your Downloads folder and sorts files into Images, Documents, and Videos folders based on their extensions.

The Problem

Saves hours of manual administrative time every week by automating digital organization.

Real-World Use Case

Saves hours of manual administrative time every week by automating digital organization.

Technology Stack

Python basics

Prerequisite

os and shutil modules

Prerequisite

Architecture & Design

Folder Structure

file-organizer/
└── organize.py

API Design

IntegrationAPI

None.

Step-by-Step Implementation

1

Import os and shutil.

### Step 1: Project Setup Create a script named `organize.py`.

python
import os
import shutil

TARGET_DIR = "/Users/username/Downloads"
EXTENSIONS = {
    "Images": [".jpg", ".jpeg", ".png", ".gif"],
    "Documents": [".pdf", ".docx", ".txt", ".xlsx"],
    "Videos": [".mp4", ".mov"]
}

def organize_files():
    for filename in os.listdir(TARGET_DIR):
        file_path = os.path.join(TARGET_DIR, filename)
        
        if os.path.isfile(file_path):
            ext = os.path.splitext(filename)[1].lower()
            
            for folder, exts in EXTENSIONS.items():
                if ext in exts:
                    target_folder = os.path.join(TARGET_DIR, folder)
                    if not os.path.exists(target_folder):
                        os.makedirs(target_folder)
                        
                    shutil.move(file_path, os.path.join(target_folder, filename))
                    print(f"Moved {filename} to {folder}")
                    break

if __name__ == "__main__":
    organize_files()

Code Explanation

Implementation step

2

Define a dictionary mapping file extensions to folder names.

### Step 2: Core Logic Use `os.listdir()` to get all files.

python
import os
import shutil

TARGET_DIR = "/Users/username/Downloads"
EXTENSIONS = {
    "Images": [".jpg", ".jpeg", ".png", ".gif"],
    "Documents": [".pdf", ".docx", ".txt", ".xlsx"],
    "Videos": [".mp4", ".mov"]
}

def organize_files():
    for filename in os.listdir(TARGET_DIR):
        file_path = os.path.join(TARGET_DIR, filename)
        
        if os.path.isfile(file_path):
            ext = os.path.splitext(filename)[1].lower()
            
            for folder, exts in EXTENSIONS.items():
                if ext in exts:
                    target_folder = os.path.join(TARGET_DIR, folder)
                    if not os.path.exists(target_folder):
                        os.makedirs(target_folder)
                        
                    shutil.move(file_path, os.path.join(target_folder, filename))
                    print(f"Moved {filename} to {folder}")
                    break

if __name__ == "__main__":
    organize_files()

Code Explanation

Implementation step

3

Iterate through all files in the target directory.

### Step 3: Moving Files Use `shutil.move()` to transfer the files.

python
import os
import shutil

TARGET_DIR = "/Users/username/Downloads"
EXTENSIONS = {
    "Images": [".jpg", ".jpeg", ".png", ".gif"],
    "Documents": [".pdf", ".docx", ".txt", ".xlsx"],
    "Videos": [".mp4", ".mov"]
}

def organize_files():
    for filename in os.listdir(TARGET_DIR):
        file_path = os.path.join(TARGET_DIR, filename)
        
        if os.path.isfile(file_path):
            ext = os.path.splitext(filename)[1].lower()
            
            for folder, exts in EXTENSIONS.items():
                if ext in exts:
                    target_folder = os.path.join(TARGET_DIR, folder)
                    if not os.path.exists(target_folder):
                        os.makedirs(target_folder)
                        
                    shutil.move(file_path, os.path.join(target_folder, filename))
                    print(f"Moved {filename} to {folder}")
                    break

if __name__ == "__main__":
    organize_files()

Code Explanation

Implementation step

4

Move each file to its corresponding folder.

### Step 4: Error Handling Handle `PermissionError` if a file is currently open.

python
import os
import shutil

TARGET_DIR = "/Users/username/Downloads"
EXTENSIONS = {
    "Images": [".jpg", ".jpeg", ".png", ".gif"],
    "Documents": [".pdf", ".docx", ".txt", ".xlsx"],
    "Videos": [".mp4", ".mov"]
}

def organize_files():
    for filename in os.listdir(TARGET_DIR):
        file_path = os.path.join(TARGET_DIR, filename)
        
        if os.path.isfile(file_path):
            ext = os.path.splitext(filename)[1].lower()
            
            for folder, exts in EXTENSIONS.items():
                if ext in exts:
                    target_folder = os.path.join(TARGET_DIR, folder)
                    if not os.path.exists(target_folder):
                        os.makedirs(target_folder)
                        
                    shutil.move(file_path, os.path.join(target_folder, filename))
                    print(f"Moved {filename} to {folder}")
                    break

if __name__ == "__main__":
    organize_files()

Code Explanation

Implementation step

Common Errors

Script tries to move directories instead of files.

Add a check for `os.path.isfile()`.

Security & Performance

Create dummy text files and verify they are moved to 'Documents'.

Run the script twice to ensure it handles already-moved files gracefully.


Run the script on a cron schedule automatically every night.

Interview Questions

Growth Newsletter

Get practical AI tools, SEO tips, and growth guides weekly.

Join creators, students, and businesses scaling with TechIdea.