From shelve documentation, i can see use Shelf.close() if you want to close a shelf; use flag='r' ensures read-only mode and flag='c' allows creation if it doesn't exist. Other flag modes are w open an existing shelf; n always create a new, empty shelf, open for reading and writing.