A file is a non-volatile (durable) container for storing data. Its contents persist even after a program ends or the computer is shut down. Files are typically organized on secondary storage devices, such as hard disk drives (HDDs) or solid-state drives (SSDs). In contrast, variables are stored in volatile memory (RAM) and are lost when the program terminates or the system restarts. On the other hand, storing data in files and retrieving it is a relatively slow process compared to accessing data in variables. To summarize, files should be used for long-term storage when the benefit of non-volatility outweighs the cost of slow access.
In this chapter, you’ll learn about different file formats, common ways to organize data, and how to read and write that data using Python. You’ll first learn how to open and read information from files. After that, you’ll learn about the different techniques for writing to files, and then you’ll see several case studies that use the various techniques.