Shelve-problems

Fredrik Lundh fredrik at pythonware.com
Sun Oct 17 07:44:16 EDT 1999


Thomas Weholt <thomas at bibsyst.no> wrote:
> I try to do something like :
> 
> >>>import shelve
> >>>db = shelve.open('2')
> >>> db['1'] = 'test'
> 
> And quit python, restart it and this happens when I try to read the data
> back in :
> 
> >>> import shelve
> >>> db = shelve.open('2')
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python1.5/shelve.py", line 152, in open
>     return DbfilenameShelf(filename, flag)
>   File "/usr/lib/python1.5/shelve.py", line 142, in __init__
>     Shelf.__init__(self, anydbm.open(filename, flag))
>   File "/usr/lib/python1.5/anydbm.py", line 83, in open
>     raise error, "db type could not be determined"
> anydbm.error: db type could not be determined
> >>> 
> 
> Why?!

not sure -- two possible causes comes to mind:

1) you didn't close the database.  I have a vague
memory that one of the database drivers won't
properly flush changes to the file if you don't
(cannot remember which one, though).

2) you don't have any database handlers on your
machine, so Python falls back to the "dumbdbm"
driver.  and while "anydbm" can fall back on the
"dumbdbm" module when creating a database, it
fails to recognize it when you reopen it...

(guess someone should fix "whichdb"...)

one way around this is to use shelve with an
explicit database, like this:

import shelve
import gdbm

def gdbm_shelve(filename, flag="c"):
    return shelve.Shelf(gdbm.open(filename, flag))

db = gdbm_shelve("dbfile")

(taken from the eff-bot library guide, see below).

</F>

<!-- coming monday:
http://www.pythonware.com/people/fredrik/librarybook.htm
(the eff-bot guide to) the standard python library. -->





More information about the Python-list mailing list