Dārayavahuš tdi Dārayavahuš tdi - 7 months ago 34
Python Question

Efficient way to store dictionary (hash) in file with python?

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 standard library module for python, but I can see it will be deprecated in Python 3. I also saw the pickle standard library module.

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


I would start with the shelve module and see if that isn't too slow. It does exactly what you want.

import shelve

d = shelve.open('filename')

d['name'] = 'path'


or to read from it

d = shelve.open('filename')

d = hash['name']

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