DEV Community

Daniil Kornilov
Daniil Kornilov

Posted on Edited on

Five small Python automation examples and five ideas to explore

Disclosure: This article was created or edited with AI assistance. It is educational guidance, not a report of measured results. No measured productivity, hiring, sales or income outcome is claimed. Examples are illustrative unless a source is explicitly given.

The code blocks are educational examples, not a verified production package. Check dependencies, platform compatibility and behavior in your own project.

Stop Doing Manually What Python Can Do in Seconds

Here are five illustrative scripts and five further ideas. Inspect paths and output before using them with your own files. These examples do not establish time savings or unattended reliability.

1. File Organizer

from pathlib import Path
import shutil

CATEGORIES = {
    'Images': {'.jpg', '.jpeg', '.png', '.gif', '.webp'},
    'Documents': {'.pdf', '.docx', '.txt', '.md'},
    'Code': {'.py', '.js', '.ts', '.swift', '.html', '.css'},
}

def organize(directory, *, dry_run=True):
    root = Path(directory).expanduser()
    for source in root.iterdir():
        if not source.is_file() or source.is_symlink():
            continue
        for category, extensions in CATEGORIES.items():
            if source.suffix.lower() not in extensions:
                continue
            target = root / category / source.name
            if target.exists():
                print(f'Skip existing target: {target}')
                break
            print(f'{source} -> {target}')
            if not dry_run:
                target.parent.mkdir(exist_ok=True)
                shutil.move(str(source), str(target))
            break

# Preview only. Review the paths before choosing dry_run=False.
organize('~/Downloads')
Enter fullscreen mode Exit fullscreen mode

2. Bulk Image Resizer

from pathlib import Path
from PIL import Image

def resize_images(folder, max_size=(1200, 1200)):
    root = Path(folder).expanduser()
    output = root / 'resized'
    output.mkdir(exist_ok=True)
    for source in root.iterdir():
        if not source.is_file() or source.suffix.lower() not in {'.jpg', '.jpeg', '.png'}:
            continue
        target = output / source.name
        if target.exists():
            print(f'Skip existing output: {target}')
            continue
        with Image.open(source) as img:
            img.thumbnail(max_size)
            img.save(target)
        print(f'Created: {target}')
Enter fullscreen mode Exit fullscreen mode

This writes separate output files. Keep originals and decide how to handle metadata, orientation and color profiles for your use case.

3. CSV to JSON Converter

import csv
import json

def csv_to_json(csv_file, json_file):
    with open(csv_file, 'r', encoding='utf-8', newline='') as f:
        reader = csv.DictReader(f)
        data = list(reader)
    with open(json_file, 'x', encoding='utf-8') as f:
        json.dump(data, f, indent=2)
Enter fullscreen mode Exit fullscreen mode

4. Website Availability Checker

import requests
import time

SITES = ['https://yoursite.com', 'https://api.yoursite.com']

def check_sites():
    for site in SITES:
        try:
            r = requests.get(site, timeout=10)
            status = f'HTTP {r.status_code}'
        except requests.RequestException:
            status = "UNREACHABLE"
        print(f"{site}: {status}")

# Run every 5 minutes with cron
check_sites()
Enter fullscreen mode Exit fullscreen mode

5. Git Stats Generator

import subprocess

def git_stats():
    commits = subprocess.run(['git', 'log', '--oneline'], capture_output=True, text=True)
    commits.check_returncode()
    total = len(commits.stdout.splitlines())

    authors = subprocess.run(['git', 'shortlog', '-sn', 'HEAD'], capture_output=True, text=True)

    print(f"Total commits: {total}")
    print(f"\nContributors:\n{authors.stdout}")

git_stats()
Enter fullscreen mode Exit fullscreen mode

6-10: Quick Wins

  • Email report sender — auto-generate and send daily reports
  • Database backup — scheduled SQLite/PostgreSQL dumps
  • Log analyzer — parse logs and alert on errors
  • API data fetcher — pull data from APIs on schedule
  • Markdown to PDF — convert docs with custom styling

The Key Principle

Consider automating a repeated task after comparing setup, review and maintenance effort with the manual work. Add error reporting before scheduling it.

Getting Started

  1. Pick your most repetitive task
  2. Write a Python script for it
  3. Schedule it with cron (Linux/Mac) or Task Scheduler (Windows)
  4. Review the first runs, add failure alerts and maintain the script when inputs change

Which tasks do you automate? Share in the comments!

Top comments (0)