Python搁置可以创建但不能打开搁架
Posted
技术标签:
【中文标题】Python搁置可以创建但不能打开搁架【英文标题】:Python shelve able to create but not open shelf 【发布时间】:2018-04-21 02:59:33 【问题描述】:我有一个奇怪的经历。我在 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 提示符下运行文件告诉我它是一个伯克利数据库。我确实安装了 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】:这很奇怪,但如果我省略文件名末尾的 .db,问题就解决了。也就是说,我在跑
shelve.open("filename.db")
并且这失败了。如果您的 python shell 中有任何类型的自动完成功能,它似乎可以使用 .db
扩展名来获取它。为了让它工作,我必须运行
shelve.open("filename")
【讨论】:
以上是关于Python搁置可以创建但不能打开搁架的主要内容,如果未能解决你的问题,请参考以下文章