Efficient way to store dictionary (hash) in file with python?
Question: I'm implementing a Unix userland tool that needs to store a hash on the disk.
The hash will be read every run of the program, pretty frequently. The hash
needs to store "name:path" values only.

I looked at the [bsddb](https://docs.python.org/2/library/bsddb.html) standard
library module for python, but I can see it will be deprecated in Python 3. I
also saw the [pickle](https://docs.python.org/3/library/pickle.html) standard
library module.

I'm not a python guy, so what is the efficient way for hash serialization and
frequent open/read/close operations?


Answer: I would start with the [shelve](http://docs.python.org/library/shelve.html)
module and see if that isn't too slow. It does exactly what you want.

    
    
    import shelve
    
    d = shelve.open('filename')
    
    d['name'] = 'path'
    
    d.close()
    

or to read from it

    
    
    d = shelve.open('filename')
    
    d = hash['name']
    

It's essentially a wrapper around pickle that provides a dictionary
abstraction.

