【发布时间】: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类型?