Section 1: Persistent Data
Table of Contents >
Chapter 6 > Section 1
To this point, data that has been produced by our programs is not
persistent. In other words, it does not exist beyond the time frame
in which our program or Python shell is running. If we want to use
it at some later time, we must make a note of it by manually copying the
values to a piece of paper or to a file. If the amount of data that
we want to persist is small, this is not hard to do but this approach is
not practical when a large amount of data is involved.
When working with spreadsheets, word processors and graphics programs, for
example, we are used to having to save our work to a file before we exit
the program. When we save work to (or write data to) a file, we are
ensuring that the data that we have produced with the program persists
after that program has terminated. At a later time, we can continue
to work with the data by loading it (or reading it in) from the file in
which it is stored.
In this section of the course we look at how to read and write data to
files. Files can be broken down into two broad categories: text
files and binary files. Text files used to store raw textual data
are such that certain characters in the file are interpreted in particular
ways. For example,
\n
is interpreted (on some systems)
as an end-of-line. So, rather than displaying
\n
on
the screen, we see a line break. Binary files, on the other hand,
are used to store image data, audio data and other data. In this
course, we will consider the use of text files only.