CodingNic

File I/O

Module Overview

File I/O 5 min read

Module Overview

File I/O

Real programs don’t just hold data in variables that vanish when the script ends: they read and write files. This module covers file I/O (input/output): reading and writing plain text files, CSVs, and JSON.

Why This Matters

CSVs and JSON are two of the most common ways data moves between programs, spreadsheets, databases, and APIs. Being able to read and write them programmatically is a core, everyday skill.

What You’ll Learn

  • Building and checking file paths with pathlib
  • Reading and writing text files, and how the file cursor works
  • The with statement, and why it’s safer than manually opening and closing files
  • The different file modes (r, r+, a, a+, w, w+)
  • Reading and writing CSV files with the csv module
  • Reading and writing JSON files and strings with the json module

Outcome

By the end of this module, you’ll be able to read data out of files, and write and update files programmatically instead of by hand. The module closes with a mini project: a personal expense tracker whose data actually persists between runs.

Let’s get started.