Persistence Options in Python
In this chapter, our focus is on persistent data -- the kind that outlives a program that creates it. That’s not true by default for objects a script constructs; things like lists, dictionaries, and even class instance objects live in your computer’s memory and are lost as soon as the script ends. To make data longer-lived, we need to do something special. In Python programming, there are at least five traditional ways to save information between program executions:
Flat files: storing text and bytes
DBM keyed files: keyed access to strings
Pickled objects: serializing objects to byte streams
Shelve files: storing pickled objects in DBM keyed files
Database systems: full-blown SQL and object database systems
We studied Python’s simple (or “flat”) file
interfaces in earnest in Chapter 2, and have been
using them ever since. Python provides standard access to both the
stdio filesystem (through the built-in
open function), as well as lower-level
descriptor-based files (with the built-in os
module). For simple data storage tasks, these are all that many
scripts need. To save for use in a future program run, simply write
data out to a newly opened file on your computer and read it back
from that file later. As we’ve seen, for more advanced tasks,
Python also supports other file-like interfaces such as pipes, fifos,
and sockets.
Since we’ve already explored flat files, I won’t say more about them here. The rest of this chapter introduces the remaining topics ...