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

Port Scan Localhost Common Ports in Python

Scan common localhost ports (HTTP, HTTPS, SSH, FTP, and more) with a fast socket-based Python script that prints an open/closed status table.

socket port-scanning network
Python
import socket
from datetime import datetime

COMMON_PORTS = {
    80: "HTTP", 
    443: "HTTPS", 
    22: "SSH", 
    21: "FTP", 
    25: "SMTP",
    3306: "MySQL",
    5432: "PostgreSQL"
}

def scan_port(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(0.1)
    try:
        resu…
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.