Python Stdlib Reference

Comprehensive reference for python stdlib reference covering essential concepts, practical examples, and production best practices.

Python Stdlib Reference is a curated guide to the core libraries included with Python, focused on the commands, modules, and idioms engineers use daily—especially when debugging, building tools, or deploying services. It details the exact behavior of standard library components with minimal abstraction, emphasizing what works, what breaks silently, and how to recognize and fix common pitfalls.

File and Path Operations

Open Files with pathlib.Path

from pathlib import Path

# Read a file
data = (Path("data") / "config.json").read_text(encoding="utf-8")

# Write a file with directory creation
(Path("logs") / "app.log").write_text("Starting service...", encoding="utf-8")

# Iterate over files with glob patterns
for file_path in Path("src").glob("*.py"):
    print(file_path.name)

File System Metadata with os.stat()

import os

stat_info = os.stat("example.txt")
print(stat_info.st_mtime)  # last modification time (float seconds since epoch)
print(stat_info.st_size)  # file size in bytes
print(stat_info.st_mode)  # file mode (e.g., 0o100644 for regular file)

Walk Directories with os.walk()

for root, dirs, files in os.walk("project"):
    for file in files:
        full_path = os.path.join(root, file)
        if file.endswith(".py"):
            print(f"Python file: {full_path}")

Text and String Processing

Regular Expressions with re

import re

# Compile pattern once for performance
pattern = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z]{2,}\b", flags=re.IGNORECASE)

# Search with full match details
match = pattern.search("Contact: [email protected] for help")
if match:
    print(match.group())  # '[email protected]'
    print(match.start(), match.end())  # 10, 34
    print(match.groups())  # ('[email protected]',)

# Substitution with function
def camel_to_snake(name):
    return re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower()

print(camel_to_snake("CamelCaseString"))  # 'camel_case_string'

String Formatting with str.format() and f-strings

name = "Alice"
age = 30

# Using str.format()
template = "Hello, {name}. You are {age} years old."
print(template.format(name=name, age=age))

# Using f-strings (preferred)
print(f"Hello, {name}. You are {age} years old.")

# f-string with expression and formatting
print(f"Next year: {age + 1} ({name.upper()})")

Data Serialization and Parsing

JSON with json

import json

# Parse JSON from string
data = json.loads('{"name": "Alice", "age": 30}')
print(data["name"])  # "Alice"

# Serialize to string
json_str = json.dumps(data, indent=2, sort_keys=True)

# Read from file
with open("config.json", "r", encoding="utf-8") as f:
    config = json.load(f)

# Write to file
with open("output.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2)
import datetime

def serialize_datetime(obj):
    if isinstance(obj, datetime.datetime):
        return obj.isoformat()
    raise TypeError(f"Object of type {type(obj)} not serializable")

json.dumps(data, default=serialize_datetime)

Pickle for Python-native Serialization

import pickle

# Serialize object
with open("data.pkl", "wb") as f:
    pickle.dump(data, f)

# Deserialize object
with open("data.pkl", "rb") as f:
    loaded_data = pickle.load(f)

# Bulk operations
with open("data.pkl", "rb") as f:
    objects = pickle.load(f)
    more_objects = pickle.load(f)  # reads next object from same file

Time and Date Handling

datetime and time

from datetime import datetime, timedelta, timezone

now = datetime.now()
utc_now = datetime.now(timezone.utc)

# Format
formatted = now.strftime("%Y-%m-%d %H:%M:%S")

# Parse
parsed = datetime.strptime("2023-10-05 14:30:00", "%Y-%m-%d %H:%M:%S")

# Time arithmetic
future = now + timedelta(days=7, hours=3)
print(future)

# Time zone handling
utc = timezone.utc
eastern = timezone(timedelta(hours=-5))
dt = datetime(2023, 10, 5, 14, 30, tzinfo=utc)
eastern_time = dt.astimezone(eastern)

time module

import time

start = time.time()
# ... work ...
elapsed = time.time() - start

# Sleep with fractional seconds
time.sleep(0.123)  # 123 milliseconds

# Get current time in seconds since epoch
epoch = time.time()

# Get time as struct
now_struct = time.localtime()
print(now_struct.tm_year)  # 2023
print(time.strftime("%H:%M:%S", now_struct))

# High-resolution timer
start_ns = time.perf_counter()
# ... work ...
elapsed_ns = time.perf_counter() - start_ns

Process and System Interaction

subprocess for Running External Commands

import subprocess

# Run command and capture output
result = subprocess.run(
    ["git", "status", "--porcelain"],
    capture_output=True,
    text=True,
    check=True,
)

print(result.stdout)  # list of changed files

# Handle non-zero exit
try:
    subprocess.run(
        ["python", "main.py"],
        check=True,
        timeout=30,
    )
except subprocess.CalledProcessError as e:
    print(f"Command failed: {e.cmd}")
    print(f"Return code: {e.returncode}")
    print(f"Output: {e.output}")
    print(f"Stderr: {e.stderr}")

# Pipe input
result = subprocess.run(
    ["grep", "error"],
    input="Error: File not found\nWarning: Low disk space\nError: Timeout",
    text=True,
    capture_output=True,
)

argparse for Command-Line Interface

import argparse

parser = argparse.ArgumentParser(description="Process data files.")
parser.add_argument("input", help="Input file path")
parser.add_argument("-o", "--output", help="Output file path", default="output.json")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output")
parser.add_argument("--limit", type=int, default=100, help="Maximum number of records to process")

args = parser.parse_args()

if args.verbose:
    print(f"Processing {args.input} -> {args.output}")

for i in range(args.limit):
    # process records
    pass

Error Handling and Debugging

logging Module

import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(name)s | %(levelname)s | %(message)s",
    handlers=[logging.FileHandler("app.log"), logging.StreamHandler()],
)

logger = logging.getLogger("main")

logger.info("Starting application")
logger.warning("Low disk space")
logger.error("Failed to connect to database")

try:
    result = 10 / 0
except Exception as e:
    logger.exception("An error occurred")

traceback for Exception Details

import traceback

try:
    1 / 0
except Exception as e:
    print(traceback.format_exc())

Working with Collections

collections Module

from collections import Counter, defaultdict, deque

# Count items
items = ["apple", "banana", "apple", "cherry", "banana", "apple"]
counts = Counter(items)
print(counts["apple"])  # 3

# Default dictionary
word_lengths = defaultdict(int)
for word in ["hello", "world", "hello"]:
    word_lengths[word] += len(word)

# Deque for efficient appends and pops from both ends
queue = deque(maxlen=10)
queue.append("first")
queue.appendleft("last")
queue.append("new")
print(queue.popleft())  # "last"

itertools for Efficient Iteration

import itertools

# Infinite iterators
cycle = itertools.cycle(["a", "b", "c"])
for i, item in enumerate(itertools.islice(cycle, 10)):
    print(f"{i}: {item}")

# Combinatorics
for combo in itertools.combinations([1, 2, 3], 2):
    print(combo)  # (1, 2), (1, 3), (2, 3)

# Grouping
data = [("A", 1), ("B", 2), ("A", 3)]
sorted_data = sorted(data, key=lambda x: x[0])
for key, group in itertools.groupby(sorted_data, key=lambda x: x[0]):
    print(f"Group {key}: {list(group)}")

Working with Iterables and Generators

zip and itertools.zip_longest

names = ["Alice", "Bob"]
ages = [30, 25]

# Regular zip
for name, age in zip(names, ages):
    print(f"{name} is {age} years old")

# Zip with fill value
from itertools import zip_longest

for name, age in zip_longest(names, ages, fillvalue="Unknown"):
    print(f"{name} is {age} years old")

Generator Functions

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

# Use generator
fib = fibonacci()
first_ten = [next(fib) for _ in range(10)]
print(first_ten)  # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

This page was rewritten on 10 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.