What I have is 3 dictionaries dumping with pickle to 3 different files. I had originally just written a read and write function for each file so a total of 6 functions. So last night I tried to make it so I would only have to use 1 read function and 1 write function. I got the write function to work but the read function is not. I have been looking since last night for a solution to this problem and I am fed up so any help you can give I would appreciate.
I am new to programming/python if you could not tell. Here is the code I’m using:
w = {} # would be past in as source
def writing(filename, source):
with open(filename, 'wb') as st:
pickle.dump(source, st)
def reading(filename, source):
with open(filename, 'rb') as st:
source = pickle.loads(st.read())
reading('test.txt', w)
The error I got is:
Traceback (most recent call last):
File "./database.py", line 303, in <module>
pw.check_pwd(p)
File "./database.py", line 47, in check_pwd
if self.pwds[self.user] == hashlib.sha512(self.pwd + self.salt).hexdigest():
KeyError: 'Codex' this was the error I was getting sorry for the bad post
I do not know what problem you are trying, but I am very confident that you want the result of the unpickling in the global
wvariable, right? Well, you cannot do it this way: when you attribute a value to thesourcevariable, you are updating only the local variablesourcebut the variablewstill points to the same value. Maybe this answer may help you to understand what is happening.There are various solutions to it. One of my favorite ones is the @katrielatex one: return the value:
Actually, you do not even need the
sourceparameter:Another solution is to update the dictionary pointed by
source. In this case, no return is needed:Also, you can make
wa global variable inside the function (but it is the worst solution):