Python 2.7.2 shelve fails on OSX

别说谁变了你拦得住时间么 提交于 2019-12-08 20:05:31

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.

shelve doesn't expect you to specify the extension on the filename. Instead it gives the file an extension based on which db was used to create the file.

So when you call shelve.open("shelve_test.dat"), you are creating a file that shelve expects to be created in using dumbdbm, but is instead created with a different (better) database manager.

Instead, you should be calling shelve.open("shelve_test"), that way shelve can choose to save the file using the correct extension. This also applies when opening an already created file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!