13 Reading and writing files

 

This chapter covers

  • Opening files and file objects
  • Closing files
  • Opening files in different modes
  • Reading and writing text or binary data
  • Redirecting screen input/output
  • Using the struct module
  • Pickling objects into files
  • Shelving objects

File operations are a common feature in the daily routines of many developers. The most common file type is the simple text file, which is used in a wide variety of situations, from raw data files to log files to source code and more. If text files can’t do the job, binary files can be used to store almost anything, often by using the struct module for specific binary formats or even the pickle and shelve modules to store and retrieve Python objects. Let’s walk through reading and writing data to files with Python.

13.1 Opening files and file objects

One truth about computing is that files are everywhere. The data we use is in files; the code we use to process it is files; and much of the time when we process data, we write the results to files. We download them, copy them, search them, attach them, encrypt them, and archive them. But most of all, the single most common thing you’ll want to do with files is open and read them.

13.2 Closing files

13.3 Opening files in write or other modes

13.4 Functions to read and write text or binary data

13.4.1 Using binary mode

13.5 Reading and writing with pathlib

13.6 Terminal input/output and redirection

13.7 Handling structured binary data with the struct module

13.8 Pickling objects to files

13.8.1 Reasons not to pickle

13.9 Shelving objects

13.10 Final fixes to wc

13.10.1 Solving the problem with AI-generated code

13.10.2 Solutions and discussion

Summary