I basically do sequences of dump and load, but at some point I want to delete one of the loaded entries. How can I do that? Is there a way to remove, or edit entries saved with Python pickle/cpickle?
Edit: The data is saved with pickle in a binary file.
To delete a pickled object from a binary file you must rewrite the whole file. The
pickle
module doesn't deal with modifications at arbitrary portions of the stream, so there is no built-in way of doing what you want.Probably the simplest alternative to binary files is to use the
shelve
module.This module provides a
dict
like interface to a database containing the pickled data, as you can see from the example in the documentation:The database used is
ndbm
orgdbm
, depending on the platform and the libraries available.Note: this works well if the data is not moved to an other platform. If you want to be able to copy the database to an other computer then
shelve
wont work well, since it does not provide guarantees regarding which library will be used. In this case using an explicit SQL database is probably the best option.