shelve error

Peter Otten __peter__ at
Mon Apr 9 03:00:10 EDT 2007

Alex Martelli wrote:

> BTW, if you DO want to call on a path f that may correspond
> to an arbitrary existing file (and want to toss away the previous
> contents of that file, if any) the correct way to call is then:
> s =, 'n')
> since 'n' truncates an existing file, or creates a new one, as needed.

It's not entirely arbitrary since you get an exception if that file is not a
valid database:

>>> open("tmp.db", "w").write("garbage")
>>> import shelve
>>>"tmp.db", "n")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.5/", line 225, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/local/lib/python2.5/", line 209, in __init__
    Shelf.__init__(self,, flag), protocol, writeback)
  File "/usr/local/lib/python2.5/", line 80, in open
    raise error, "db type could not be determined"
anydbm.error: db type could not be determined


More information about the Python-list mailing list