Python搁置能够创建但不能打开架子

时间:2018-04-21 02:59:34

标签: python shelve

我有一种奇怪的经历。我在debian上运行python3.5.3,通过官方存储库安装。我有一些物品存放在货架上,如下所示

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

当我这样做时,它将其保存为filename.db。在unix提示符下运行此文件告诉我它是一个Berkeley数据库。我确实安装了bsddb模块。

然后当我尝试:

s1 = shelve.open(filename)

我收到以下错误:

<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

1 个答案:

答案 0 :(得分:0)

这很奇怪,但如果我从文件名末尾省略.db ,问题就解决了。也就是说,我正在运行

shelve.open(filename.db)

这是失败的。如果你的python shell中有任何类型的自动完成,它似乎用.db扩展名来获取它。为了让它工作,我必须运行

shelve.open(filename)