Python Scripts That Automate My Daily Tasks
Introduction
I reach for Python when a task has three properties: it involves file manipulation, text processing, or API calls; I need it done in an hour not a day; and I'd rather not maintain a full application for it. Python's standard library is remarkably complete for these jobs, and when the standard library falls short, the ecosystem fills the gap quickly.
Over the past few years I've built up a small collection of automation scripts that run regularly and save me meaningful time. This post shares the most useful ones — with working code, explanations of the key patterns, and notes on what made each one worth building.
Core Concepts
Python for Automation: Key Stdlib Modules
pathlib— modern, readable file path manipulationsubprocess— running shell commandsargparse— building CLI interfacesjson,csv— data parsingre— regular expressionsdatetime— date and time handlingshutil— high-level file operations (copy, move, archive)logging— proper logging (much better than print statements)os— OS-level operations (environment variables, directory creation)
These cover 80% of automation use cases before you need pip install anything.
Script 1: Bulk File Organiser
This script organises a messy downloads folder by file extension into categorised subdirectories. I run it weekly.
#!/usr/bin/env python3
"""
organise.py — Sort files into directories by extension.
Usage: python organise.py ~/Downloads
"""
import argparse
import shutil
from pathlib import Path
from datetime import datetime
# Mapping of extensions to category names
CATEGORIES = {
'images': {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico', '.bmp'},
'documents': {'.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.txt', '.md'},
'code': {'.py', '.js', '.ts', '.tsx', '.jsx', '.html', '.css', '.json', '.yaml', '.yml'},
'archives': {'.zip', '.tar', '.gz', '.rar', '.7z', '.bz2'},
'videos': {'.mp4', '.mov', '.avi', '.mkv', '.webm'},
'audio': {'.mp3', '.wav', '.flac', '.aac', '.ogg'},
}
def get_category(extension: str) -> str:
ext = extension.lower()
for category, extensions in CATEGORIES.items():
if ext in extensions:
return category
return 'misc'
def organise_directory(source: Path, dry_run: bool = False) -> dict[str, int]:
moved = {}
skipped = 0
for item in source.iterdir():
if item.is_dir():
continue # Don't touch directories
category = get_category(item.suffix)
dest_dir = source / category
if not dry_run:
dest_dir.mkdir(exist_ok=True)
# Handle name collisions
if item.name in dest_dir.iterdir():
dest_dir = dest_dir / f"{item.name}.copy"
moved[item.name] = dest_dir
skipped += 1
return moved- Automatizacija fajlova
- Web scraping
- Task scheduling
More information: Real Python