user128063
user128063

Reputation: 203

Python shelve able to create but not open shelf

I'm having a strange experience. I'm running python3.5.3 on debian, installed through the official repositories. I have a few objects that I store in a shelf as follows

with shelve.open(filename,'c') as shelf:
    shelf['lists'] = (bus1,bus2)
    shelf['N'] = N
    shelf['wtfun'] = mywtfun.__name__
    shelf['g'] = g
    shelf['svs'] = mysvs

It saves it as filename.db when I do this. Running file on this in the unix prompt tells me it is a Berkeley database. I do have the bsddb module installed.

Then when I try:

s1 = shelve.open(filename)

I get the following error:

<ipython-input-100-171f41c7dda0> in <module>()
----> 1 s1 = shelve.open('busemanns-runs-30000-N-1000-absnormal-2018-04-20T22:14:18.439245.shelf.db')

/usr/lib/python3.5/shelve.py in open(filename, flag, protocol, writeback)
241    <ipython-input-100-171f41c7dda0> in <module>()
----> 1 s1 = shelve.open('busemanns-runs-30000-N-1000-absnormal-2018-04-20T22:14:18.439245.shelf.db')

/usr/lib/python3.5/shelve.py in open(filename, flag, protocol, writeback)
    241     """
    242
--> 243     return DbfilenameShelf(filename, flag, protocol, writeback)

/usr/lib/python3.5/shelve.py in __init__(self, filename, flag, protocol, writeback)
225     def __init__(self, filename, flag='c', protocol=None, writeback=False):
226         import dbm
--> 227         Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
228
229

/usr/lib/python3.5/dbm/__init__.py in open(file, flag, mode)
 86     elif result == "":
 87         # db type cannot be determined
---> 88         raise error[0]("db type could not be determined")
 89     elif result not in _modules:
 90         raise error[0]("db type is {0}, but the module is not "

error: db type could not be determined
 """
    242
--> 243     return DbfilenameShelf(filename, flag, protocol, writeback)

/usr/lib/python3.5/shelve.py in __init__(self, filename, flag, protocol, writeback)
    225     def __init__(self, filename, flag='c', protocol=None, writeback=False):
226         import dbm
--> 227         Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
228
229

/usr/lib/python3.5/dbm/__init__.py in open(file, flag, mode)
 86     elif result == "":
 87         # db type cannot be determined
---> 88         raise error[0]("db type could not be determined")
 89     elif result not in _modules:
 90         raise error[0]("db type is {0}, but the module is not "

error: db type could not be determined

Upvotes: 2

Views: 777

Answers (1)

user128063
user128063

Reputation: 203

This is pretty weird, but the problem is fixed if I omit the .db from the end of the filename. That is, I was running

shelve.open("filename.db")

and this was failing. If you have any sort of autocomplete in your python shell, it seems to pick it up with the .db extension. To get it to work, I had to run

shelve.open("filename")

Upvotes: 4

Related Questions