Python 2.7.2搁置在OSX上失败 [英] Python 2.7.2 shelve fails on OSX

查看:107
本文介绍了Python 2.7.2搁置在OSX上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python 2.7.2上的标准库,我编写了一个非常简单的测试来创建持久数据文件,然后立即将其打开以进行打印:

Using the shelve standard lib on Python 2.7.2, I wrote an extremely simple test to create a persistent data file and then immediately open it for printing:

import os
import shelve

shelf_filename = str(__file__.split('.')[0] + '.dat')

#delete the shelf file if it exists already.
try:
    os.remove(shelf_filename)
    print "DELETED LEFTOVER SHELF FILE", shelf_filename
except OSError:
    pass

#create a new shelf, write some data, and flush it to disk
shelf_handle = shelve.open(shelf_filename)
print "OPENED", shelf_filename, "WITH SHELF"
shelf_handle['foo'] = 'bar'
shelf_handle.close()
print "FLUSHED AND CLOSED THE SHELF"

#re-open the shelf we just wrote, read/print the data, and close it
shelf_handle = shelve.open(shelf_filename)
print "RE-OPENED", shelf_filename, "WITH SHELF"
print 'foo:', shelf_handle.get('foo')
shelf_handle.close()

#delete the shelf file
os.remove(shelf_filename)

但是,当此脚本尝试重新打开刚刚创建的文件架时,该脚本意外失败:

However this script fails unexpectedly when it tries to re-open the shelf it just created:

DELETED LEFTOVER SHELF FILE shelve_test.dat
OPENED shelve_test.dat WITH SHELF
FLUSHED AND CLOSED THE SHELF
Traceback (most recent call last):
  File "shelve_test.py", line 21, in <module>
    shelf_handle = shelve.open(shelf_filename)
  File ".../shelve.py", line 239, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File ".../shelve.py", line 223, in __init__
    Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
  File ".../anydbm.py", line 82, in open
    raise error, "db type could not be determined"
anydbm.error: db type could not be determined

这是关于搁架的最基本的用法,因此我真的不明白我所缺少的内容,而AFAIK我正在按照文档中的说明去做.可以给有一点搁置经验的人告诉我发生了什么事吗?

This is about the most basic possible usage of shelve so I really don't understand what I'm missing, and AFAIK I'm following the docs to the letter. Can somebody with a little shelve experience please tell me what's going on?

更新:该脚本显然可以在某些平台上使用,而不能在其他平台上使用,这对于Python标准库来说确实是令人惊讶的.绝对确认这不适用于

UPDATE: This script apparently works in certain platforms and not others, which is really kind of surprising for a Python standard lib. Definitely confirmed this does NOT work on:

Python 2.7.2 (default, May 15 2013, 13:46:05) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin

推荐答案

此版本的Python搁置使用了不推荐使用的dbm软件包,可以通过指定其他dbm来解决此问题,如下所示:

This version of Python shelve is using a deprecated dbm package, and the issue can be solved by specifying a different dbm as follows:

import anydbm
anydbm._defaultmod = __import__('dumbdbm')

如果将这两行添加到上面的脚本中,那么一切都会按预期进行.这确实需要搁置.

If those two lines are added to the script above then everything works as expected. This really needs to be fixed in shelve.

这篇关于Python 2.7.2搁置在OSX上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆