Python I/O workflow

Python File Handling with CSV and JSON

Read and write text, CSV, and JSON files with context managers, encoding control, and schema checks that catch bad rows before reporting.

How this page is maintained

Written for learners, checked against the sources below, and reviewed every year. Last reviewed July 22, 2026.

Short answer

Use with open(...) as f to manage files safely, then parse with the csv or json modules depending on format. Always validate row counts and required fields before treating loaded data as production-ready.

  • Context managers close files automatically even when errors occur.
  • Validate schema assumptions right after reading files.
  • Write output atomically when replacing important artifacts.

Read files with explicit encoding and schema checks

Text and CSV files should be opened with an explicit encoding such as utf-8 to avoid environment-specific behavior. For CSV data, verify required headers before processing rows.

For JSON payloads, check that expected top-level keys exist and that values have the right shape before downstream transformations.

Write output safely and predictably

When generating reports, write to a temporary file first and rename once complete. This prevents partial files if a script stops mid-write.

  • Use newline="" with csv module writing to avoid blank lines on some platforms.
  • Log output paths and record counts for auditability.
  • Keep raw input files unchanged and write derived data separately.

Load orders.csv and write a clean summary

A daily orders.csv file contains order_id, order_date, channel, and amount.

  1. Open orders.csv with csv.DictReader and validate required columns.
  2. Convert amount values to float and skip rows with invalid required fields.
  3. Write channel-level totals to summary.csv using csv.DictWriter.
Result: The script produces a clean summary file with documented skips and no locked file handles.

Common mistakes

  • Opening files without a context manager and leaking handles.
  • Relying on default encoding and getting environment-specific read failures.
  • Writing output into the same file that is currently being read.
  • Ignoring malformed rows without logging what was skipped.

Try one

What is the practical benefit of using with open(...) as f in Python file handling?

The file is closed automatically, which avoids leaks and keeps behavior safe during exceptions.

Sources

Learn this with a tutor

Tell LearnLive what you already know and what you need to do with file handling.

Build this course