Standard library tour
pathlib, os, shutil, subprocess, datetime and zoneinfo, json, collections, itertools, re, random and more.
Python's motto is "batteries included": the standard library ships with hundreds of modules, available everywhere Python is installed, with no pip install required. Knowing what's in the box saves you from reinventing wheels (and from adding dependencies you don't need). This lesson is a guided tour of the modules working developers reach for every week. Bookmark the official docs at docs.python.org/3/library for the full list.
pathlib: files and folders#
pathlib.Path is the modern way to work with filesystem paths — objects with methods, instead of strings and scattered functions.
Handy methods: glob("*.csv") (one folder), rglob("*.py") (recursive), read_text/write_text, unlink() (delete a file), rename(), stat().st_size, resolve() (absolute path). On Windows the paths print with backslashes — that's the point: your code stays the same.
os and shutil: the operating system#
os gives access to environment variables, processes and lower-level file operations; shutil does high-level file operations like copying and deleting whole trees.
Reading configuration and secrets from environment variables (rather than hard-coding them) is standard practice for anything you deploy. Be careful with shutil.rmtree — there is no recycle bin.
subprocess: running other programs#
Pass the command as a list of arguments (not one string with shell=True) to avoid shell-injection bugs. check=True raises CalledProcessError if the program fails.
datetime and zoneinfo: dates and times#
Rules that save you from time-zone bugs:
- A datetime without
tzinfois naive — Python has no idea which zone it's in. Prefer aware datetimes. - Store and compute in UTC (
datetime.now(timezone.utc)); convert to local zones only for display. - Use
zoneinfo.ZoneInfo("Asia/Kolkata")(Python 3.9+) rather than fixed offsets — it knows about daylight saving changes. (On Windows,pip install tzdataprovides the zone database.) - ISO 8601 strings (
isoformat()/fromisoformat()) are the best format for storage and APIs.strftime/strptimehandle custom formats.
json: data interchange#
JSON gets its own lesson next, together with CSV.
collections: specialised containers#
itertools and functools#
product replaces nested loops, combinations and permutations solve many interview puzzles, and functools gives you partial, reduce, cache and wraps.
re: regular expressions (preview)#
Regular expressions get a full lesson in the advanced section.
random, secrets, math and statistics#
Use random for games and simulations, but secrets for passwords, tokens and anything security-related.
A few more worth knowing#
Common mistakes#
- Reinventing the wheel — before writing a helper, check
itertools,collections,pathlibandfunctools. - Naive datetimes across time zones.
randomfor security — usesecrets.subprocesswithshell=Trueand user input — command injection risk.- Naming your files after stdlib modules (
random.py,json.py), which shadows them.
What's next#
Two data formats come up in almost every job: CSV spreadsheets and JSON from APIs. Next lesson: working with CSV and JSON.
Check your understanding
Quick quiz
1.Which is the modern, cross-platform way to build the path
data/reports/june.csv?2.What's the safest way to store a timestamp for an event in a web app?
3.What does
collections.Counter("banana").most_common(1)return?
Finished reading?
Mark this lesson complete to track your progress.