Automation & scripting
CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.
How to Build a Docker Image Tag Script in Python
Generate consistent Docker image tags from service names and versions with automatic normalization.
#!/usr/bin/env python3
"""Mock script for building docker image tags."""
def build_tag(service_name: str, version: str, registry: str = "docker.io") -> str:
"""Construct a docker image tag."""
safe_name = service_name.lower().replace("_", "-")
return f"{registry}/{safe_name}:{version}"
if __name__ == "…
How to Generate an Inventory CSV of Installed pip Packages in Python
This script uses subprocess and csv to list all installed pip packages and write their names and versions into a CSV inventory file.
import subprocess
import csv
def get_installed_packages():
"""Return a list of (name, version) tuples for installed pip packages."""
result = subprocess.run(
["pip", "list", "--format=freeze"],
capture_output=True,
text=True,
check=True
)
packages = []
for line in r…
Pin Python package versions in requirements.txt
Pin package versions in requirements.txt-style text by adding ==version when no specifier is present, while preserving existing version constraints and comments.
import re
from pathlib import Path
def pin_versions(requirements_text: str) -> str:
"""
Pin package versions in requirements.txt-style text.
Adds ==version if no version specifier is present.
Keeps existing specifiers (>=, <=, ~=, etc.) unchanged.
"""
lines = requirements_text.strip().splitli…
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.