Python Story

Chapter #8 – Libraries

standard library overview: os, sys, pathlib, datetime, re, math

8.0 Prologue

Python ships with a rich standard library — "batteries included." This chapter surveys the modules you will reach for most often, giving you a working mental map before the detailed I/O and collections chapters.

8.1 os and sys

os provides operating system interfaces: environment variables, process management, directory operations, and path manipulation. sys exposes the Python interpreter itself: command-line arguments, standard streams, and the module search path. import os, sys home = os.environ.get("HOME", "/tmp") pid = os.getpid() cwd = os.getcwd() print(sys.argv) # ['script.py', 'arg1', ...] print(sys.version) # '3.12.0 (main, ...)' sys.exit(0) # exit with code Prefer pathlib over os.path for new code; os is still useful for process-level operations.

8.2 pathlib

pathlib.Path represents filesystem paths as objects, making path manipulation readable and cross-platform: from pathlib import Path p = Path.home() / "projects" / "app" p.mkdir(parents=True, exist_ok=True) cfg = p / "config.json" if cfg.exists(): text = cfg.read_text(encoding="utf-8") for f in p.glob("**/*.py"): print(f.name, f.stat().st_size) Key methods: read_text(), write_text(), read_bytes(), write_bytes(), glob(), rglob(), stat(), rename(), unlink(), rmdir().

8.3 datetime

The datetime module provides types for dates, times, and durations: from datetime import date, datetime, timedelta, timezone today = date.today() now = datetime.now(tz=timezone.utc) delta = timedelta(days=30) future = now + delta # formatting and parsing formatted = now.strftime("%Y-%m-%d %H:%M:%S") parsed = datetime.strptime("2026-01-01", "%Y-%m-%d") For timezone-aware code, use datetime.now(tz=timezone.utc) and the third-party zoneinfo module (built in since Python 3.9) for named time zones.

8.4 re — Regular Expressions

The re module provides Perl-compatible regular expressions. Compile patterns for reuse: import re email_re = re.compile(r"[\w.+-]+@[\w-]+\.[a-z]{2,}", re.IGNORECASE) text = "Contact alice@example.com or bob@test.org" matches = email_re.findall(text) # ['alice@example.com', 'bob@test.org'] m = email_re.search(text) if m: print(m.group(), m.start(), m.end()) cleaned = re.sub(r"\s+", " ", "too many spaces") Key functions: match(), search(), findall(), finditer(), sub(), split().

8.5 math and random

math provides mathematical functions on floats (faster than the equivalent Python operations for scalars). random provides pseudo-random number generation: import math, random print(math.sqrt(2)) # 1.4142135623730951 print(math.log(math.e)) # 1.0 print(math.isclose(0.1 + 0.2, 0.3)) # True random.seed(42) print(random.randint(1, 6)) # die roll print(random.choice(["a", "b", "c"])) deck = list(range(52)) random.shuffle(deck) sample = random.sample(deck, 5) # 5 unique draws For cryptographic randomness use secrets; for statistical distributions use random.gauss(), random.uniform(), etc.

8.6 logging

The logging module provides a flexible framework for emitting log messages with configurable handlers and levels. Prefer it over print() for production code: import logging logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(levelname)s %(name)s: %(message)s" ) log = logging.getLogger(__name__) log.info("application started") log.warning("disk usage high: %d%%", 92) log.error("connection failed", exc_info=True) Levels in ascending order: DEBUG, INFO, WARNING, ERROR, CRITICAL. Configure handlers (file, rotating, syslog) and formatters to route messages appropriately.

8.7 Key Third-Party Packages

Package Purpose
requestsHTTP client with a simple API
httpxasync-capable HTTP client
numpyN-dimensional arrays and math
pandastabular data analysis (DataFrame)
pydanticdata validation using type hints
pytesttesting framework
fastapimodern async web API framework
sqlalchemyORM and database toolkit
Install packages with pip install <package> inside a virtual environment. Pin versions in requirements.txt or pyproject.toml.

8.8 Epilogue

This chapter surveyed frequently used standard library modules and key third-party packages. The next two chapters dive deeper into the I/O and collections libraries.

8.9 References

Python standard library — python.org
pathlib — python.org
logging — python.org
PyPI — Python Package Index