Python Mastery · Chapter 10 · ~24 min · Intermediate
Files, APIs & Automation Scripts
From writing error-free cross-platform file scripts to streaming gigabyte CSV logs, parsing JSON responses, and building bulletproof REST API clients that never hang or crash your production cron jobs.
- Handle files safely with pathlib and with
- Process CSV and JSON data
- Call REST APIs with timeouts
- Build automation that retries and logs
1Modern File I/O: pathlib & Context Managers
Historically, Python developers wrote f = open('file.txt') and stitched paths together with messy string concatenation. In modern Python, pathlib.Path and the with context manager make file operations clean, cross-platform, and leak-proof.
File Buffer & Context Manager Inspector
Watch how OS RAM buffers hold unwritten bytes until with flushes and releases the file handle.
Buffer empty. Waiting for write() or read() call.
__exit__() automatically triggers flush() and close() even if an exception occurs.user_id,login_time,status 101,08:30:12,success
from pathlib import Path
path = Path("audit.csv")
with path.open("w", encoding="utf-8") as f:
f.write("...")
with open(...), Python automatically flushes and releases the file descriptor even if an exception aborts your script!2Structured Data Pipelines: CSV & JSON
Automation scripts constantly ingest comma-separated logs or export structured JSON payloads for monitoring dashboards. The standard library modules csv and json provide fast, zero-dependency tools.
CSV & JSON Dual Pipeline Lab
Switch between tabular CSV streaming (csv.DictReader) and structured JSON serialization (json.dumps).
user_id,username,role,active 1001,ada_lovelace,admin,True 1002,alan_turing,engineer,True 1003,grace_hopper,architect,False
csv.DictReader reads the first line as dictionary keys, eliminating manual index offsets like row[0].
Safe & readable: [row["username"] for row in reader if row["active"] == "True"]
Reading Tabular CSV:
import csv
from pathlib import Path
path = Path("users.csv")
with path.open("r", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["username"], row["role"])Writing Formatted JSON:
import json
from pathlib import Path
payload = {"status": "ok", "count": 42}
Path("output.json").write_text(
json.dumps(payload, indent=2),
encoding="utf-8"
)Try it with real files
Open your CSV in the CSV Viewer & Editor, inspect API output in the JSON Viewer, and test endpoints in the HTTP Request Builder — all offline-friendly.
Why use with open(...) instead of open() alone?
3Consuming REST APIs with requests & httpx
Real-world automation almost always connects to an external web service: sending Slack notifications, triggering GitHub deployments, or polling internal microservices.
HTTP REST Automation Simulator
Test headers, status codes, and exceptions with Python's requests and httpx.
{
"uptime_pct": 99.98,
"active_workers": 8,
"requests_per_sec": 1420
}Equivalent Python requests call:
response = requests.get(
"https://api.cloudmetrics.internal/api/v1/metrics",
headers={"Authorization": f"Bearer {os.getenv('API_KEY')}"},
timeout=5.0 # Crucial! Prevents indefinite hangs
)
response.raise_for_status() # Raises HTTPError on 4xx/5xx
data = response.json()requests.get(url, timeout=10.0). If the remote server stops responding mid-socket, a script without an explicit timeout will hang indefinitely, freezing your cron job!A cron script calls requests.get(url) with no timeout and the server hangs. What happens?
4Resilient Pipelines: Backoff, Retries & Logging
Networks fail, rate limits happen, and APIs experience brief outages. A brittle script crashes and wakes up on-call engineers; a resilient script logs the hiccup, backs off, and succeeds on a subsequent attempt.
Resilient Automation Script Runner
Real automation scripts survive transient network errors with retries, status checks, and logging.
Queued
Queued
Queued
Queued
Click "Trigger Cron Script" to execute the pipeline.
5The 4 Fatal Traps of Python Automation
Before scheduling an automation script in production, review the four most common pitfalls that cause silent data corruption, credential leaks, or memory exhaustion.
The 4 Fatal Traps of Python Scripting & Automation
Compare dangerous anti-patterns with production-grade resilient practices.
Hardcoded API Secrets in Code
Never hardcode tokens or passwords in script files. Use os.getenv('API_KEY') or python-dotenv so scripts can be safely committed to version control.
API_KEY = "sk_live_99812491283" # LEAK RISK! # Checked into public git repository!
import os
from pathlib import Path
# Reads from environment variable or .env file
API_KEY = os.environ.get("API_KEY")
if not API_KEY:
raise RuntimeError("API_KEY environment variable is required")6Challenge: Automation Architect
Test your mastery of file pointers, cross-platform path handling, JSON serialization, HTTP status verification, and memory-safe stream iteration. Score 5/5 to complete Chapter 10!
Files, APIs & Automation Scripts · code lab
Edit the example, run it with Python, or step through the lines.
1import json2from pathlib import Path34report = {"course": "Python", "complete": True}5path = Path("/tmp/course-report.json")6path.write_text(json.dumps(report))7loaded = json.loads(path.read_text())8print(loaded["course"], loaded["complete"])
Names
Objects in memory
Python output
Run the code to see its output.
Automation Architect Challenge
Pick an answer — you get feedback instantly. Get every question right to finish the chapter.
1Why should you always open files with 'with open(...) as f:' rather than 'f = open(...)'?
2What is the key difference between json.loads() and json.load() in Python?
3Your automation script uses requests.get(url). Why must you call response.raise_for_status()?
4How should an automation script process a 50 Gigabyte access log file without running out of RAM?
5What is the best way to construct cross-platform file paths and read content in modern Python 3.8+?
Run and customize the live file parser & JSON reporting pipeline in the Python Playground (code preloaded).
7Recap
- pathlib.Path: Use object-oriented, cross-platform paths with
/,.read_text(), and.write_text(). - Context Managers: Always open files with
with path.open() as f:to guarantee closing and flushing. - Lazy Line Streaming: Never call
.read()on giant files; iterate directly withfor line in f:. - csv.DictReader & json: Ingest CSV headers as dictionary keys, and serialize JSON with
json.dumps(). - API Reliability: Always set explicit
timeout=...and callresponse.raise_for_status(). - Secret Safety: Pull API credentials from
os.getenv('KEY'), never hardcoded in source.
Finish the challenge above to complete the chapter, or