0

I'm working with the shelve library, on Python 3.8.3., and I'm fairly confident with creating new entries from a database, and reading a certain key's values. However, I can't do something like this:

users = shelve.open("users", "r")
name = input()
if name in users:
    do something
else:
    do something else
users.close()

Since I don't have access to the dictionary directly, but to a shelve object. I suppose I could turn the keys into a list, but that would defeat the whole purpose of using a dictionary. Any ideias?

J. Dionisio
  • 159
  • 1
  • 13
  • @VictorS, the name is supposed to be a user's username, and the shelve's keys are the names. So, for example, if you had already registered under "Victor", users["Victor"] would have some information about your account. – J. Dionisio Jul 04 '20 at 13:30
  • @VictorS yes, that's right! – J. Dionisio Jul 04 '20 at 13:37
  • @VictorS, users is a shelve object. But, as a dictionary, it should look like {n1: ['asd', 0, 0], n2:['fs',12,421]...} – J. Dionisio Jul 04 '20 at 13:50

1 Answers1

0

It appears that I am very dumb. I was reading from a file called "users" and writing to a file called "users.txt", and that's what was where the mistake was coming from. You can do what I said in the original post, if name in users....

The error message was not very indicative of this error though (of a key error), so I'll leave the question up for posterity.

J. Dionisio
  • 159
  • 1
  • 13