我使用的是Python shelve模块和以下Python版本:
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32
。的flag参数搁架打开描述方法here:
The optional flag parameter has the same interpretation as the flag parameter of dbm.open().
dbm flag参数描述为here:
'n' Always create a new, empty database, open for reading and writing
但以下程序并没有像我预期的那样工作:
import shelve import os import os.path import shutil myshelvedir='C:\\testdir' myshelvefile=os.path.join(myshelvedir,'myshelve') os.mkdir(myshelvedir) try: myshelve=shelve.open(myshelvefile) try: myshelve['key1']='value1' print(1,dict(myshelve)) finally: myshelve.close() myshelve=shelve.open(myshelvefile,'n') try: myshelve['key2']='value2' print(2,dict(myshelve)) finally: myshelve.close() finally: shutil.rmtree(myshelvedir)
结果是
^{pr2}$我期待的第二条线
2 {'key2': 'value2'}
因为搁置架是用'n'
标志打开的,因此打开后数据库应该是空的。在
我错过什么了吗?在
附加测试:
打开
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32
我也得到
2 {'key2': 'value2', 'key1': 'value1'}
打开
Python 2.6.4 (r264:75706, Jun 27 2012, 05:45:50) [C] on sunos5
我得到了预期的结果
(2, {'key2': 'value2'})
也在
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win 32
我明白了
(2, {'key2': 'value2'})
所以这是3的一个新特性或者一个bug
我找到了http://bugs.python.org/issue18039。在
给出了一个解释。在
文档显示shelve使用了一个可用的数据库接口。在
http://docs.python.org/3/library/shelve.html
“n”标志的工作方式与
dbm.open
中的类似http://docs.python.org/3/library/dbm.html
但是n-flag的这个定义与n-flag的定义相矛盾,如果它回到数据库管理系统.dumb在
http://docs.python.org/3/library/dbm.html#module-dbm.gnu
http://docs.python.org/3/library/dbm.html#module-dbm.dumb说
因此,在Unix实现中,这可能不是一个问题gnu.dbm系统库已经安装(我无法访问Unix上的python3来测试它)
由此我得出结论,python文档中存在一些相互冲突的语句: 在安装了dbm库的unix系统上,python shelve将使用此dbm安装,这里的“n”标志的工作方式如文档中所述。在
如果可以在windows上找到安装(例如)windows数据库管理器“n”的工作方式与“c”标志类似时,将使用模块。在
相关问题 更多 >
编程相关推荐