Python搁架打开(…,flag='n')不创建空sh

2024-09-27 21:32:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是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


Tags: importosonbitopenshelveflagkey2
1条回答
网友
1楼 · 发布于 2024-09-27 21:32:53

我找到了http://bugs.python.org/issue18039。在

Title: dbm.open(..., flag="n") does not work and does not give a warning

给出了一个解释。在

文档显示shelve使用了一个可用的数据库接口。在

http://docs.python.org/3/library/shelve.html

12.3. shelve — Python object persistence
...
shelve.open(filename, flag='c', protocol=None, writeback=False)
Open a persistent dictionary.
...
The optional flag parameter has the same interpretation as the flag parameter of dbm.open().
...
12.3.1. Restrictions The choice of which database package will be used (such as dbm.ndbm or dbm.gnu) depends on which interface is available. Therefore it is not safe to open the database directly using dbm.

“n”标志的工作方式与dbm.open中的类似

http://docs.python.org/3/library/dbm.html

12.5. dbm — Interfaces to Unix “databases”
dbm is a generic interface to variants of the DBM database — dbm.gnu or dbm.ndbm. If none of these modules is installed, the slow-but-simple implementation in module dbm.dumb will be used
...
'n' : Always create a new, empty database, open for reading and writing

但是n-flag的这个定义与n-flag的定义相矛盾,如果它回到数据库管理系统.dumb在

http://docs.python.org/3/library/dbm.html#module-dbm.gnu

12.5.1. dbm.gnu — GNU’s reinterpretation of dbm
Platforms: Unix

http://docs.python.org/3/library/dbm.html#module-dbm.dumb

12.5.3. dbm.dumb — Portable DBM implementation
...
dbm.dumb.open(filename[, flag[, mode]]) ... The optional flag argument is currently ignored; the database is always opened for update, and will be created if it does not exist.

因此,在Unix实现中,这可能不是一个问题gnu.dbm系统库已经安装(我无法访问Unix上的python3来测试它)

由此我得出结论,python文档中存在一些相互冲突的语句: 在安装了dbm库的unix系统上,python shelve将使用此dbm安装,这里的“n”标志的工作方式如文档中所述。在

如果可以在windows上找到安装(例如)windows数据库管理器“n”的工作方式与“c”标志类似时,将使用模块。在

相关问题 更多 >

    热门问题