liveBook · Manning

Chapter 13: Reading and writing files

What you will learn in chapter 13 of The Quick Python Book, Fourth Edition

This introduction explains why developers work with text and binary files and identifies Python modules used for particular kinds of binary storage.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why developers work with text and binary files and identifies Python modules used for particular kinds of binary storage.

Key terms: text file, binary file, struct module, pickle, shelve

13.1 Opening files and file objects

This section explains how opening a file creates a file object, how successive reads get lines from it, and how to supply paths and use the preferred opening style.

Key terms: open, pathname, file object, file I/O, readline, newline character, with, context manager, I/O errors, current working directory

13.2 Closing files

This section explains why file objects should be closed and how to close them explicitly or automatically with `with`.

Key terms: file object, close, system resources, close method, with, context manager

13.3 Opening files in write or other modes

This section explains how the mode argument to open controls reading, writing, and appending, and briefly introduces optional file-handling parameters.

Key terms: open, 'r', 'w', 'a', buffering, encoding, newline characters

13.4 Functions to read and write text or binary data

This section compares ways to read and write file contents, including their memory use and treatment of newline characters.

Key terms: readline, readlines, file object, iterator, text mode, universal mode, newline parameter, binary mode, write, writelines

13.4.1 Using binary mode

Explains how to read file data as bytes in binary mode and when those bytes must be decoded into strings.

Key terms: binary mode, 'rb', read method, bytes object, bytes objects, string objects, decode

13.5 Reading and writing with pathlib

This section shows how Path objects read and write text or binary files without explicit open and close calls, while noting that writing replaces existing content.

Key terms: Path object, read_bytes, write_bytes, text, binary mode, append

13.6 Terminal input/output and redirection

This section explains how to read terminal input and direct printed or stream output to files, including how to restore standard output afterward.

Key terms: input, prompt, int, sys.stdin, sys.stdout, sys.stderr, standard input, standard output, standard error, redirect

13.7 Handling structured binary data with the struct module

The section shows when the struct module is useful and how a format string lets Python write and read structured binary records.

Key terms: struct module, binary data, pickling, format string, C short integer, C double-precision floating-point number, 'hd7s', struct.pack, struct.calcsize, read

13.8 Pickling objects to files

This section explains how pickle saves and restores Python objects, how to choose what to persist, and how a persistent cache can reuse results across program runs.

Key terms: pickle module, pickling, unpickling, byte stream, pickle.dump, pickle.load, shared objects, cyclic references, code objects, system resources

13.8.1 Reasons not to pickle

This section explains why pickle can be a poor choice for storing objects, especially when a file might be altered by someone else.

Key terms: pickling, serialization, JSON, arbitrary code

13.9 Shelving objects

This section explains how shelves provide persistent, dictionary-like access to objects on disk, why they help with large datasets, and where their limits matter.

Key terms: shelve.open, shelf object, persistent dictionary, file accesses, lookups, strings as keys, pickled, concurrent access, close

13.10 Final fixes to wc

This exercise asks readers to make the Python wc utility process either a named file or redirected standard input.

Key terms: wc utility, input redirection, command-line argument, stdin, argparse, nargs

13.10.1 Solving the problem with AI-generated code

This subsection frames the final wc refactoring as an AI-assisted task and explains why a harder byte-versus-character counting feature was left out.

Key terms: wc utility, standard input, standard output, -c, -m, bytes, characters, binary mode, text mode

13.10.2 Solutions and discussion

13.11 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 12: Using the filesystem Chapter 14: Exceptions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.