Reference library

Automation & scripting

CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.

2 matches
Automation & scripting easy

How to Tail and Colorize Error Lines in Python

Reads the last N lines of a log file and prints error lines in red using ANSI color codes.

logging terminal colorize
Python
import sys
import time
from pathlib import Path

def tail_colorize(filename: str, lines: int = 20) -> None:
    """Read last N lines of a file, printing errors in red."""
    path = Path(filename)
    if not path.exists():
        print(f"File '{filename}' not found.", file=sys.stderr)
        return

    # Read last …
15 0 Open
Automation & scripting easy

Monitor Disk Usage and Alert in Python

A Python script that checks disk usage percentage against a threshold and returns an ALERT or OK message with free space details.

disk monitoring shutil
Python
import shutil
import os

def check_disk_usage(path="/", threshold=85.0):
    usage = shutil.disk_usage(path)
    percent_used = (usage.used / usage.total) * 100
    
    if percent_used > threshold:
        return (f"ALERT: Disk usage at {percent_used:.1f}% on {path} "
                f"(exceeds {threshold}% threshold…
12 0 Open

Browse by section

Each section groups closely related Python snippets.

Automation & scripting — Python code examples

What you will find here

This page collects automation & scripting snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.