Python 5.3Code walkthrough9 parts

Working with Files

Read and write text, CSV and JSON files.

Loading your progress…Next: 5.4 Exceptions

In this lesson9 parts

  1. 01The reviews in a file
  2. 02Paths
  3. 03Writing and reading text
  4. 04Modes
  5. 05Encodings
  6. 06CSV
  7. 07JSON
  8. 08The reviews, counted from the file
  9. 09Recap, your challenge, and next

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

CSV
A table stored as text, one row per line, values separated by commas. A value with a comma in it is written in quotes.
encoding
The rule that turns text into bytes, and bytes back into text. This course always uses UTF-8.
file object
What open gives back; you read from it or write to it. with closes it.
JSON
A text format for dictionaries, lists, strings and numbers. It has no tuples.
path
The folders and the file name that locate a file: Path("data") / "notes.txt".

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 2 · Files and Exceptions.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.