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.pyAPI Design
APINone.
Step-by-Step Implementation
Import os and shutil.
### Step 1: Project Setup Create a script named `organize.py`.
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
Define a dictionary mapping file extensions to folder names.
### Step 2: Core Logic Use `os.listdir()` to get all files.
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
Iterate through all files in the target directory.
### Step 3: Moving Files Use `shutil.move()` to transfer the files.
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
Move each file to its corresponding folder.
### Step 4: Error Handling Handle `PermissionError` if a file is currently open.
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
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.