Introduction

You've been opening files on a computer for years. You double-click a document, drag a photo into an email, save a spreadsheet and open it again next week. What you probably haven't done is write the code that does any of that.

That's what this book is about. Not files as an idea, but the ordinary job of getting your data out of a file and back into one, in Python, using only what comes with the language.

What a file is

A file is a run of bytes on a disk, with a name. The bytes might spell the text of a note, the rows of a spreadsheet, or the pixels of a photo. The name is how you find them again later.

Python doesn't know or care what the bytes mean. When you open a file, Python hands you the bytes, or the text they spell out, and the rest is up to you. Most of the trouble people have with files lives in that handoff. The file holds bytes. Your program wants a list of ratings. Something has to get from one to the other, and that something is most of what this book covers.

Two questions

Every chapter answers a version of one of two questions:

  • How do I get data out of a file?
  • How do I put data into a file?

Reading and writing. Paths, encodings, CSV, and JSON are all detail hanging off those two.

The reading log

Rather than invent a fresh example for every chapter, the book uses one small set of files all the way through: a personal reading log.

  • reading-log.csv has one row per book, with its title, author, your rating out of five, and the date you finished it. A couple of books have no rating yet because you're still reading them.
  • notes/ holds a short text file for some of the books, with whatever you scribbled down when you finished.
  • settings.json holds a couple of preferences: how many books you want to read this year, and which formats you count.

Over the chapters you'll read this log, add to it, fix a broken character in it, split it by year, and tidy up the notes folder. By the last chapter you'll have built a small script that does all of that in one go.

The files are in the book's download, in a folder called fixtures. Copy that folder somewhere you can experiment, and work from the copy.

What you need

  • Python 3.11 or newer.
  • A terminal you can run python in, and a plain text editor. Any editor is fine.
  • Nothing else. No packages to install.

To check your version, run this:

import sys

version = f"{sys.version_info.major}.{sys.version_info.minor}"
print(f"You're running Python {version}.")
Output
You're running Python 3...

If that number is 3.11 or higher, you're set. The examples were built and tested on the version printed on the copyright page. Yours might be a little newer, which is fine.

How the examples work

Every block of Python in this book was run while the book was built. If a snippet is here, it worked and produced the output shown under it.

books_this_year = 9
goal = 24
print(f"{books_this_year} of {goal}, {goal - books_this_year} to go")
Output
9 of 24, 15 to go

A grey Output panel like that one shows exactly what the snippet prints, so you can check your own result against it. Chapters also have Try it boxes with a small change to make, and end with a Practice section: a few problems and their worked solutions.

One more time, because the preface said it and it matters: type the examples out rather than pasting them, and break them on purpose. You learn the most from the parts your fingers stumble on and from the error messages you cause.