2016-03-03 25 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