2016-03-03 62 views
1

为什么搁置如果我尝试打开一个刚刚创建的搁置文件会引发错误?搁置数据库类型无法确定,whichdb不能识别gdb

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.pyopen方法提高。

我知道它使用gdbm。我检查了whichdb.py文件,并尝试找出这个

# 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" 

但在我的文件中的“魔术”号是3245083670x13579acf)(只有最后一个数字的变化!)GDBM文件

我试着用另一种语言(红宝石)打开文件,我能打开它没有任何问题,因此这似乎是在whichdb.py错误试图找出正确的DBM

+0

使用Python 2.7.6此代码的工作没有问题。 – 2016-03-03 12:28:06

+0

请编辑你的问题,以包括它正在提出的确切的错误。 – msw

+0

在Windows 3.5上正常工作。 – msw

回答

1

上解释这个错误的问题是由于在哪个db中无法识别的错误造成的ify一些最新的GDB文件,更多信息在这个错误报告:https://bugs.python.org/issue13007

最好的解决办法是强制数据库定义一个方法,用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) 
+0

此解决方案给了我另一个错误,我在这个问题中描述:http://stackoverflow.com/q/39553264/4038337。 – cnaak