Python Data Automation · Chapter 4 of 10

File Handling

Read and write text, CSV, and JSON files safely with context managers, encoding control, and schema checks.

Why this chapter matters

Reliable file handling prevents data corruption, leaked file handles, and silent parsing errors in automation pipelines.

What you will learn

  • Use with open(...) to manage file lifecycle safely.
  • Parse CSV and JSON with standard library tools.
  • Validate required fields before downstream processing.

Understand the core ideas

File handling is a reliability layer in automation. Using with open(...) ensures files are closed even when errors occur, which prevents resource leaks and locked handles. Explicit encoding such as utf-8 avoids machine-specific defaults that can break parsing in different environments. For CSV and JSON, parse into well-defined structures and validate required columns or keys before transformation. If input schemas drift silently, downstream logic can produce incorrect outputs that appear plausible, which is often harder to detect than an immediate failure.

A production mindset treats file boundaries as untrusted inputs and outputs. Check file existence, catch decode errors, and emit clear error messages that include file path and failed field names. For writing outputs, use deterministic naming and write complete artifacts atomically when possible, so partial writes do not masquerade as successful runs. Keep error handling practical: raise precise exceptions from IO functions and let orchestration code decide whether to retry, skip, or fail fast. This separation preserves both clarity and operational control.

Key terms

context manager
A construct such as with open that guarantees setup and cleanup around file operations.
encoding
The text byte representation, for example utf-8, used during reading and writing.
schema check
Validation that required columns or keys exist before processing continues.
atomic write
A write pattern that avoids leaving a partially written output as the final artifact.

Clean and summarize CSV input with guarded file IO

You need to read orders.csv, verify required columns order_id, channel, and amount, then write summary.csv with revenue by channel. Input quality is mixed and files may be missing or malformed.

  1. Open the input using with open(input_path, encoding='utf-8', newline='') and csv.DictReader. Catch FileNotFoundError and UnicodeDecodeError to report actionable failure reasons.
  2. Validate header fields before reading rows. If required columns are missing, raise ValueError that names missing fields and stops processing.
  3. Iterate rows, parse amount with float inside try and except, and route malformed rows to an error list. Aggregate valid revenue totals by channel in a dictionary.
  4. Write summary rows to a temporary file with DictWriter, then rename to summary.csv. Log row counts for processed, invalid, and exported records.
Result: The job either fails clearly at boundaries or produces a complete summary artifact with traceable counts. Invalid records are isolated for follow-up instead of contaminating totals or crashing late.

A common misconception

Claim: If a file opens successfully, the data is valid enough to continue.

Correction: Opening only confirms access, not schema or value quality. Always validate headers and critical fields before downstream transformations.

Lessons in this chapter

  1. Context managersOpen files safely and close them automatically.
  2. CSV workflowsRead and write tabular files with predictable formatting.
  3. JSON workflowsLoad and validate structured payloads.
  4. Guide: file handlingProduce clean derived artifacts from raw files. Read the full guide →

Study task

Load orders.csv, validate required columns, and write a cleaned summary.csv that includes total revenue by channel.

Chapter checkpoint

Why is explicit encoding (for example utf-8) important when opening files?

It avoids environment-dependent behavior and reduces read or parse failures across systems.

Learn this with an AI teacher that starts from what you already know.

Tell LearnLive your goal and starting point, and it adapts the explanations, examples, and practice as you go.

Teach me this