Python Shelve module not working: "db type cannot be defined"

I am trying to create a simple program for storing passwords in Python, and it seems pretty simple, so I am wondering if I am using shelve incorrectly.

I have a main .py file:

import shelve passwords = shelve.open('./passwords_dict.py') choice = raw_input("Add password (a) or choose site (c)?") if choice[0] == 'a': site_key = raw_input("Add for which site? ").lower() userpass = raw_input("Add any info such as username, email, or passwords: ") passwords[site_key] = userpass else: site = raw_input("Which site? ").lower() if site in passwords: print "Info for " + site + ": " + passwords[site] else: print site, "doesn't seem to exist!" print "Done!" passwords.close() 

And another passwords_dict.py file is just an empty dictionary.

But when I try to run the program, I get this error:

 Traceback (most recent call last): File "passwords.py", line 3, in <module> passwords = shelve.open('passwords_dict.py') File "/usr/lib/python2.7/shelve.py", line 239, in open return DbfilenameShelf(filename, flag, protocol, writeback) File "/usr/lib/python2.7/shelve.py", line 223, in __init__ Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback) File "/usr/lib/python2.7/anydbm.py", line 82, in open raise error, "db type could not be determined" anydbm.error: db type could not be determined 

When I try to use anydbm instead, I get this error:

 Traceback (most recent call last): File "passwords.py", line 3, in <module> passwords = anydbm.open('passwords_dict.py') File "/usr/lib/python2.7/anydbm.py", line 82, in open raise error, "db type could not be determined" anydbm.error: db type could not be determined 

And when I try to use dbm instead, I get this error:

 Traceback (most recent call last): File "passwords.py", line 3, in <module> passwords = dbm.open('./passwords_dict.py') dbm.error: (2, 'No such file or directory') 

What am I doing wrong? Is there another way to store the dictionary and still be able to retrieve the keys using user input (and not the entire dictionary, which, I suppose, is what makes it pickled)?

+6
source share
3 answers

I think you do not understand how the shelf module works. It opens a database file. When trying to open an existing file containing a Python script, it tries to determine what type of database the file contains (since the shelf supports several databases).

I think instead you want something like this:

 import os import shelve curdir = os.path.dirname(__file__) passwords = shelve.open(os.path.join(curdir, 'password_db')) 

This will create a new file in the same directory as your script called password_db.<db> , where <db> is the database file extension for a particular implementation.

+5
source

I also experienced this problem. This seems to be due to undocumented conditions for the filename shelve.open argument. It is currently very opaque (for example, shelve.open("/tmp/tmphTTQLda") works, but shelve.open("/tmp/tmphTTQLd") not). It is impossible to predict the failure and success of variable file names. I requested an explanation in the form of improved documentation at http://bugs.python.org/issue23174 .

In my case, opening a constant bit outside the shelve and passing it to shelve.Shelve works, for example. code

 a = dumbdbm.open(tempfile.mkstemp()[1]) b = shelve.Shelf(dict=a) 

and do with b what you would do with the return value shelve.open .

0
source

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)) 

Additional information on this subject: type shelve db cannot be determined, whichdb does not recognize gdb

0
source

Source: https://habr.com/ru/post/945642/


All Articles