Hour 11. Using Files
What You’ll Learn in This Hour:
File types that Python can handle
How to open a file
Reading a file’s data
Writing data to a file
Storing strings, lists, dictionaries, and so on in memory is fine for small Python scripts. However,
when writing large scripts, you need to store the data in files. In this hour, you will explore how to
use various files in your Python scripts.
Understanding Linux File Structures
Python can deal with various operating systems’ file structures. It can also handle the input and output
for text files, binary files, compressed files, and so on. If you want a language with great file-handling
capabilities that is also cross-platform, Python is your language.
Table 11.1 lists a few file types that Python can handle. Keep in mind that this is not a complete list!
TABLE 11.1 A Few File Types Python Can Process
Notice in Table 11.1 that the file types can overlap. For example, a numeric text file can be
compressed after it is created. The primary purpose of this table is to show that Python is extremely
flexible in its ability to handle various file formats.
By the Way: Overwhelming?
Don’t feel overwhelmed by the different file types in Table 11.1. The focus this hour is
on handling text files. (You can breathe a sigh of relief now.)
The various file types that Python can handle “live” in various places in the Raspbian directory
structure. Their type or purpose dictates their placement within the structure.
Looking at Linux Directories