Shelve: db type cannot be determined

I am using Pycharm. First of all, whenever a module is imported into Pycharm. The complete import line disappears. But in the case of import shelve does not disappear. Also, when I run the file, I get the following errors:

 Traceback (most recent call last): File "/Users/abhimanyuaryan/PycharmProjects/shelve/main.py", line 13, in <module> s = shelve.open("file.dat") File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/shelve.py", line 239, in open return DbfilenameShelf(filename, flag, protocol, writeback) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/shelve.py", line 223, in __init__ Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/dbm/__init__.py", line 88, in open raise error[0]("db type could not be determined") dbm.error: db type could not be determined 

Here is my code:

 import shelve s = shelve.open("file.dat") s["first"] = (1182, 234, 632, 4560) s["second"] = {"404": "file is not present", "googling": "Google to search your content"} s[3] = ["abhilasha", "jyoti", "nirmal"] s.sync() print(s["first"]) print(s["second"]) print(s[3]) 
+7
python shelve
source share
2 answers

The OP explains in a comment that 'file.dat' was created by pickle - and that is the problem! pickle does not use any database format - it uses its own! Create file.dat with shelve first (for example, run shelve when file.dat does not already exist and save the stuff in it) and everything will be ok.

OP in the comment: "I still do not understand what the problem is in this case." Answer: the problem is that pickle may not create a file in any of the DB shelve formats. Use one module for serialization and deserialization - either just pickle or just shelve - and it will work much better shelve .

+5
source share

There is one error with anydb https://bugs.python.org/issue13007 that cannot use the correct identifier for gdbm files.

So, if you are trying to open a valid gdbm file with racks, and this is what the error uses this instead:

  mod = __import__("gdbm") file = shelve.Shelf(mod.open(filename, flag)) 
+1
source share

All Articles