【问题标题】:shelve db type could not be determined, whichdb is not recognizing gdb无法确定搁置 db 类型,whichdb 无法识别 gdb
【发布时间】:2016-03-03 12:07:59
【问题描述】:

如果我尝试打开刚刚由搁置创建的文件,为什么搁置会引发错误?

import shelve
info_file_name = "/Users/bacon/myproject/temp/test.info"

info_file = shelve.open(info_file_name)
info_file['ok'] = 'wass'
info_file.close()

info_file = shelve.open(info_file_name) # raise exception db type could not be determined..
info_file.close()

我正在运行 python 2.5 以防万一

准确的错误是:

db type could not be determined 由anydbm.py open 方法引发。

我知道它使用 gdbm。我检查了 whichdb.py 文件,它试图用这个来识别 gdbm 文件

 # Read the start of the file -- the magic number
s16 = f.read(16)
s = s16[0:4]

# Convert to 4-byte int in native byte order -- return "" if impossible
(magic,) = struct.unpack("=l", s)

# Check for GNU dbm
if magic == 0x13579ace:
    return "gdbm"

但我文件中的“神奇”数字是324508367 (0x13579acf)(只有最后一个数字变化!!)

我尝试用另一种语言(ruby)打开文件,我能够毫无问题地打开它,所以这似乎是一个错误,其中 db.py 试图识别正确的 dbm

【问题讨论】:

  • 使用 Python 2.7.6 这个代码没有问题。
  • 请编辑您的问题以包含它引发的确切错误。
  • 在 Windows 上的 Python 3.5 中运行良好。
  • 我编辑了这个问题,我说的是python 2.5。我无法更改 python 版本。
  • 我知道该文件使用gdbm,是否可以强制搁置使用它而不是尝试猜测db类型?

标签: python shelve


【解决方案1】:

正如问题中所解释的,此错误是由于 whichdb 无法识别某些最新的 gdb 文件的错误,有关此错误报告的更多信息:https://bugs.python.org/issue13007

最好的解决方案是强制 db 定义一个使用 gdbm 加载搁置的方法,而不是尝试猜测 dbm。

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

然后用它代替shelve.open:

info_file = gdbm_shelve(info_file_name)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2012-06-26
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
相关资源
最近更新 更多