A practical, production-oriented reference covering frequently used Python standard library modules and a foundational guide to building RESTful APIs with Flask.
| Module | Primary Purpose | Common Use Cases | Key Functions / Classes | Example |
|---|---|---|---|---|
os |
Operating system interaction | File and directory management, environment variables | getcwd(), listdir(), remove(), mkdir(), environ |
os.listdir('.') |
sys |
Python runtime interaction | CLI arguments, interpreter info, exiting programs | argv, exit(), path, version |
sys.argv |
math |
Mathematical operations | Scientific math, rounding, constants | sqrt(), ceil(), floor(), pi, sin() |
math.sqrt(16) |
datetime |
Date and time handling | Timestamps, scheduling, formatting dates | datetime, date, time, timedelta, strftime() |
datetime.now() |
json |
JSON encoding and decoding | APIs, configuration files, data exchange | dump(), dumps(), load(), loads() |
json.loads(data) |
random |
Random value generation | Simulations, sampling, shuffling | random(), randint(), choice(), shuffle() |
random.randint(1, 10) |
re |
Regular expressions | Pattern matching, validation, parsing | search(), match(), findall(), sub() |
re.findall(pattern, text) |
collections |
Specialized data structures | Counting, queues, structured dictionaries | Counter, deque, defaultdict, namedtuple |
Counter(list) |
shutil |
High-level file operations | Copying and moving files, directory trees | copy(), copytree(), move(), rmtree() |
shutil.copy(src, dst) |
argparse |
Command-line parsing | CLI tools, automation scripts | ArgumentParser, add_argument(), parse_args() |
parser.parse_args() |
- Managing files and directories
- Reading environment variables
- Handling cross-platform file system operations
import os
print(os.getcwd())
os.mkdir("new_folder")
print(os.environ.get("HOME"))- Reading command-line arguments
- Exiting programs safely
- Inspecting interpreter configuration
import sys
print(sys.argv)
sys.exit(0)- Scientific computing
- Geometric calculations
- Rounding and working with constants
import math
print(math.pi)
print(math.sqrt(25))- Logging and auditing
- Scheduling operations
- Performing time-based calculations
from datetime import datetime, timedelta
now = datetime.now()
print(now.strftime("%Y-%m-%d"))
print(now + timedelta(days=7))- Working with APIs
- Storing configuration files
- Serializing structured data
import json
data = {"name": "Alice", "age": 30}
json_string = json.dumps(data)
print(json.loads(json_string))- Simulations and modeling
- Sampling datasets
- Randomized selections
import random
print(random.randint(1, 100))
items = ["a", "b", "c"]
print(random.choice(items))- Input validation
- Log parsing
- Text extraction and transformation
import re
text = "Email: test@example.com"
pattern = r"\S+@\S+"
print(re.findall(pattern, text))
- Frequency counting
- Efficient queue operations
- Structured dictionary defaults
from collections import Counter, deque
print(Counter([1, 1, 2, 3]))
queue = deque([1, 2, 3])
queue.appendleft(0)
- Copying entire directory trees
- Moving files
- Removing directories safely
import shutil
shutil.copy("file.txt", "backup.txt")
- Building CLI tools
- Writing automation scripts
- Creating production-ready command-line utilities
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--name")
args = parser.parse_args()
print(f"Hello {args.name}")
- Use
argparse,os, andshutiltogether to build automation tooling. - Combine
jsonanddatetimefor structured logging systems. - Pair
collections.Counterwithrefor log analytics. - Use
systo make scripts robust and production-ready for CLI deployment.
This section serves as a compact operational reference for scripting, automation, backend services, and infrastructure tooling.