Python Intermediate: Objects, Errors and Files
Files, CSV and JSON
In this lesson you will
- Read and write text files safely with the with statement
- Work with paths using pathlib
- Read and write CSV and JSON data
Introduction
Variables vanish when a program ends. To keep information, programs write it to files. In this lesson you will read and write text, and then use two formats that almost every system understands: CSV (tables) and JSON (structured data).
A note about this page: Python runs inside your browser here, with a small temporary file system that starts empty and disappears when you leave. The code is the same as on your own computer, where it would use real files.
Writing and reading a file
Use open(name, mode). The modes are "w" (write, replacing the file), "a" (append) and "r" (read, the default).
Always open files with with. It closes the file for you, even if there is an error.
\n means "new line". You must add it yourself with write, though print adds it for you.
Quick quiz
Why open files using a with statement?
Reading line by line
Looping over the file reads one line at a time, which also works for files too big to hold in memory. .strip() removes the trailing new line.
Appending
Handling missing files
A missing file raises FileNotFoundError, which you can handle with try and except.
Paths with pathlib
pathlib treats paths as objects, which is neater than joining strings and works on every operating system.
CSV: tables as text
CSV stands for comma-separated values, the format spreadsheets can export. Use the csv module so quotes and commas inside values are handled properly.
Notice that Leeds, UK was wrapped in quotes automatically, and that values read from CSV are always text, so we convert the score with int().
JSON: structured data
JSON is the standard way programs and web services exchange data. It looks like Python dictionaries and lists, and the json module converts between them.
Note how True became true and None became null, since JSON has its own spellings. To save to a file, use json.dump(data, f) and json.load(f):
Bad JSON raises json.JSONDecodeError (a ValueError), so wrap loading data you do not control in try.
Quick quiz
What does json.dumps do?
Try it yourself
Write a small to-do list program: save a list of dictionaries like {"task": "...", "done": False} to todo.json, load it back, mark the first task as done, save it again, and print the tasks that are not done.
Summary
- Open files with
with open(name, mode) as f. Modes:"r","w"and"a". - Loop over a file to read it line by line. Handle
FileNotFoundErrorwithtry. pathlib.Pathmakes paths and simple reads and writes neat.- The
csvmodule handles tables andjsonconverts between Python objects and JSON text.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading