Chapter 10. Files, Databases, Networks, and Contexts
Files and the filesystem are central to the way modern OSs work. Many OS resources are visible as part of the filesystem. For example, the Linux /dev/mem
is a view into the processor's memory, implemented as a device visible in the filesystem. Python provides file objects that map to these OS features.
At a fundamental level, OS files are simply collections of bytes. In practice, we often work with files that are collections of Unicode characters. Python offers both views of files. With some file formats, we need to process the bytes. With text files, we expect Python to properly decode Unicode characters from the bytes.
A Python file object will generally be entangled with an OS resource. In order to be sure that an application doesn't leak OS resources, we often use a context manager. This allows us to be sure that OS resources are released when Python files are closed. The with
statement provides a tidy way to work...