Python 无法确定搁置数据库类型,该类型的数据库无法识别gdb

Python 无法确定搁置数据库类型,该类型的数据库无法识别gdb,python,shelve,Python,Shelve,如果我试图打开刚由shelve创建的文件,为什么shelve会引发错误 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

如果我试图打开刚由shelve创建的文件,为什么shelve会引发错误

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()
我正在运行Python2.5,以防与此相关

准确的误差是:

无法确定数据库类型其由
anydbm.py
open
方法引发

我知道;s使用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)打开文件,并且我能够毫无问题地打开它,因此这似乎是whichdb.py试图识别正确的dbm的一个错误,正如在问题上所解释的,这个错误是由于whichdb中的一个错误,它无法识别一些最新的gdb文件,有关此错误报告的更多信息:

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

def gdbm_shelve(filename, flag="c"):
    mod = __import__("gdbm")
    return shelve.Shelf(mod.open(filename, flag))
然后使用它而不是
搁置。打开

info_file = gdbm_shelve(info_file_name)

使用Python 2.7.6这段代码可以毫无问题地工作。请编辑您的问题以包含它引发的准确错误。在windows上的Python 3.5中工作正常。我编辑了问题,正如我所说的是Python 2.5。我无法更改python版本。我知道该文件使用了
gdbm
,可以强制shelve使用它,而不是尝试猜测db类型?此解决方案给了我另一个错误,我在这个问题中描述了:。