Python File Organization: Automate Your Digital Life
Your Downloads folder is a mess. Here is how to fix it permanently with Python.
import os, shutil
from pathlib import Path
class FileOrganizer:
def __init__(self, target_path):
self.path = Path(target_path)
self.rules = {
"Documents": [".pdf", ".docx", ".txt", ".xlsx"],
"Images": [".jpg", ".png", ".gif", ".svg", ".webp"],
"Code": [".py", ".js", ".html", ".css", ".java"],
"Archives": [".zip", ".rar", ".7z", ".tar"],
"Videos": [".mp4", ".avi", ".mkv"],
}
def organize(self):
moved = 0
for folder, exts in self.rules.items():
(self.path / folder).mkdir(exist_ok=True)
for ext in exts:
for f in self.path.glob(f"*{ext}"):
if f.is_file():
shutil.move(str(f), str(self.path / folder / f.name))
moved += 1
return moved
# Run it
org = FileOrganizer("C:/Users/YourName/Downloads")
moved = org.organize()
print(f"Organized {moved} files!")
Get the complete automation toolkit with 35+ scripts:
Python Automation Toolkit - $9.99
File Organizer Suite - $7.99
Follow for daily Python tutorials!
Top comments (0)