Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You may already be familiar with the notion of files, which are named storage compartments on your computer that are managed by your operating system. The last major built-in object type that we’ll examine on our object types tour provides a way to access those files inside Python programs.
In short, the built-in open function creates a Python file object,
which serves as a link to a file residing on your machine. After
calling open, you can transfer
strings of data to and from the associated external file by calling
the returned file object’s methods.
Compared to the types you’ve seen so far, file objects are somewhat unusual. They’re not numbers, sequences, or mappings, and they don’t respond to expression operators; they export only methods for common file-processing tasks. Most file methods are concerned with performing input from and output to the external file associated with a file object, but other file methods allow us to seek to a new position in the file, flush output buffers, and so on. Table 9-2 summarizes common file operations.