file access

Ian Parker parker at gol.com
Wed Feb 21 17:40:14 EST 2001


In article <3a8e53e2$0$25475$7f31c96c at news01.syd.optusnet.com.au>, Ben
de Luca <c941520 at alinga.newcastle.edu.au> writes
>i am building a database in python its quite small only a few hundred
>entries? i gues dictionaries are the best way to store it in memory
>
>when i write the data to a file whats the best way? I guess there is no way
>to sumb the dictonaries strait to disk? i will have to parse it?
>
>

You may be able to use the "anydbm' module which gives you dictionary-
like access to files.  There's a restriction -  the keys must be strings

import anydbm
        # create the database
db = anydbm.open("data.db", "c")
        # set some keys and values
db["where"] = "here"
db["when"] = "now"
        # close it - it's now safely on disk
db.close()

And when you want to access that data again perhaps in another program,
in read-only or in read/write, just open the database accordingly and
use it like a dictionary

db = anydbm.open("data.db", "r")
for key in db.keys(): print key,":", db[key]

I guess the overhead might be high, but it's so convenient.

Regards

-- 
Ian Parker



More information about the Python-list mailing list