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.