Implement a “Save Game” Feature in Python with the shelve Module

That’s it for the basics. Think of a shelf object as a single dictionary that you store all of your game state variables in when you save a game, and then read all the game state values out of when you load a game. The shelve module handles all the file I/O details for you.

The Function

There are a few options you might want to pay attention to for the function. There are three optional parameters to the function you might want to pass, but the default values for these are what you want in 99% of the cases, so you can skip the rest of this section.

Show the optional parameter explanations. »

The flag parameter can be the string ‘r’ (to open the shelf file as read-only), ‘w’ (to open the shelf file for reading and writing), ‘c’ (to open for reading and writing, but also creating the shelf file if it doesn’t already exist. This is the default.), and ‘n’ (delete the old shelf file and start with a new, blank shelf file). I don’t really find this too handy, but there might be cases where it is useful.

The protocol parameter is probably the one parameter you want to set. It defines the version that the “pickler” uses to store the data. (“Pickling” is the term used for converting values in your variables to text that are written out to files, which is done when you save a game. “Unpickling” is the reverse. That’s what happens when you load a saved game. The shelve module makes use of the code in the more primitive (but more flexible) pickle module.)

By default the call to uses the oldest, least efficient version. This is good because it also ensures that it will be the most widely supported no matter what version of Python people are using.

However, if you want more efficiency, you can pass the integer 2 for this parameter (the versions are currently 0, 1, and 2). Or if you have “import pickle” in your script, you can pass pickle.HIGHEST_PROTOCOL and this will always use the latest version of the pickling protocol. But be warned, earlier versions of Python may not be able to read the shelf files produced by later versions of Python running your game script.

To be safe, just use the default. There’s more info about pickle protocol versions at

The third optional parameter to is the writeback parameter. This is used for a situation like this:

import shelve

shelfFile ='some_file')

myList = [1, 2, 3]

shelfFile['list'] = myList



In the above code, what is stored in the shelf file is the list [1, 2, 3] and not [1, 2, 3, 4]. The data that is stored in the shelf file is not updated even though the list that was placed in the shelfFile was updated. You need to think of the line shelfFile['list'] = myList as “Store the value in myList as it looks right now.”

If you want the shelf file to update whenever mutable values such as lists or dictionaries are updated, pass True for the writeback parameter. If you use‘some_file’, writeback=True) in the above code, then the list that is stored in the shelf file will be [1, 2, 3, 4].

Having writeback adds some memory costs to your program, and since for saved games we usually open a shelf file, write to it, and close it immediately, it’s not a very useful feature. You can ignore it.

You can read the official documentation for and its parameters.

File Formats and Shelf File Extensions

About the file formats that the shelve module uses:

It doesn’t matter. The shelve module handles all the details. You can ignore it completely. It’s one less thing you need to worry about so you can get back to making your game.

You may notice that when you create shelf files, there are actually three files that are created. If you used ‘some_file’ in the call, the files that appear on your hard drive after you call the close() method are some_file.bak, some_file.dir, and some_file.dat.

You don’t really need to know what these files are used for. In my own experiments, you can delete the .bak file (I guess it’s just a backup file, but keep it around anyway) but the .dat and .dir files are needed. The only reason I point this out is because if you want to copy your saved game files to another computer, you need to know that there is more than one file that needs to be copied.

If you pass an extension to like ‘some_file.txt’, then the files will be some_file.txt.bak, some_file.txt.dat, and some_file.txt.dir.

Security Warning

Page 2 of 3 | Previous page | Next page