ITA*_*ITA 5 python shelve python-3.7
shelve只读模式坏了吗?文档说该flag参数的工作原理如中所述,dbm.open所以我认为如果我以读取模式打开,我不应该能够更改搁置对象。
这里的页面似乎还建议修改以只读方式打开的搁置对象应该引发异常。但我仍然可以执行以下操作:
\n\nPython 3.7.2 (default, Dec 29 2018, 06:19:36) \n[GCC 7.3.0] :: Anaconda, Inc. on linux\nType "help", "copyright", "credits" or "license" for more information.\n>>> import shelve\n>>> with shelve.open(\'testdata\') as shelf:\n... shelf[\'two\'] = 2222\n... shelf[\'one\'] = 1111\n... \nRun Code Online (Sandbox Code Playgroud)\n\n接下来我将用 和 打开它flag=\'r\',只是writeback=False为了确定一下。但我可以修改对象。
>>> with shelve.open(\'testdata\', flag=\'r\', writeback=False) as shelf:\n... for k, v in shelf.items():\n... print(\'Key: \', k, \' Value: \', v)\n... shelf[\'two\'] = 1111\n... shelf[\'one\'] = 2222\n... \nKey: one Value: 1111\nKey: two Value: 2222\nRun Code Online (Sandbox Code Playgroud)\n\n只是为了确认,再次打开并打印它表明该对象确实发生了变化:
\n\n>>> with shelve.open(\'testdata\', flag=\'r\', writeback=False) as shelf:\n... for k, v in shelf.items():\n... print(\'Key: \', k, \' Value: \', v)\n... \nKey: one Value: 2222\nKey: two Value: 1111\nRun Code Online (Sandbox Code Playgroud)\n\n我缺少什么?dbm这可能与不同系统上的选择/实现有关吗?在链接页面上运行代码也不会导致:ERROR: cannot add item to database正如页面所说的那样。
\xe2\x80\x94
\n\n更新:链接页面中的代码按预期工作,即引发和错误, 当我使用早期版本的 Python
\n\nPython 3.6.7 (default, Oct 22 2018, 11:32:17)\n[GCC 8.2.0] on linux\nType "help", "copyright", "credits" or "license" for more information.\nRun Code Online (Sandbox Code Playgroud)\n\n以及在 MacOS 上:
\n\nPython 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)\n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin\nType "help", "copyright", "credits" or "license" for more information.\nRun Code Online (Sandbox Code Playgroud)\n\nUbuntu 18.04 上的 3.7.2 出现问题。如果文件名具有扩展名“.db”,则会给出:
\n\ndbm.error: db type is dbm.gnu, but the module is not available\nRun Code Online (Sandbox Code Playgroud)\n\n如果没有扩展名,只读模式将不起作用。
\n