I have recently asked a question and received an answer that I must 'pickle' my code. As a beginner, I have no idea how to do that. This was my code:
users = []
users.append([username, password])
usersFile = open("users.txt","w+")
for users in users:
usersFile.write("%s" % users)
usersFile.close()
def loginFunction():
userEntry = ""
foundName = 0
while userEntry == "":
userEntry = raw_input("Enter your username: ")
usersFile = open("users.txt", "r")
lines = usersFile.readlines()
for i, user in enumerate(users):
if userEntry == users[index][0]:
foundName = 1
passwordEntry = raw_input("Enter your password: ")
if passwordEntry == users[index][1]:
print "Username and passwords are correct"
break
else:
print "incorrect"
userEntry = ""
if foundName == 0:
print "Username not recognised"
userEntry = ""
My problem was that I was never receiving an "Enter your password" despite knowing that the username I was entering was in fact in the text file in that list. I kept getting "Username not found back" even though I knew it was there. There was mention of not having referred back to lines but again, not sure how I would go about doing that. If someone could help me out by changing up my code and explaining that would be fantastic. I have been referred to documents trying to explain it and have done by own research on the topic but none of it makes sense to me. A written example in this context would help me out a lot.
So, if I understood correctly by looking at the other two questions (1 and 2) you made related to this, your problem has two parts:
One is generating a file with a list of user/passwords and the second is using that file to do a "login" system.
The problem with writing to files is that you can regard a file as just text... It doesn't really retain the concept of a Python list
so you need to figure out a way to convert your fancy users
list of lists to text and then back to a list
of lists so you can actually use it.
There are many pre-made serializing formats. Here are a few: JSON, CSV, YAML, or the one another user recommended in another question, Pickle
Since in another post you mentioned that you're using this for learning purposes, let's try to keep it as simple as possible ok?
Let's split your exercise in two python files: One to just generate the passwords file and the other that tries to read and verify the username/password that the user entered.
So... You have a list
of username/password pairs, and you have to transform that to text so you can store it in a file. Let's just go through each entry in the list and write it to a file. How about using a bit of inspiration from Linux and use the semicolon character (;
) to mark the separation between username and password on each line of the file? Like this:
sample_users = [
["user1", "password1"],
["user2", "password2"],
["user3", "password3"]
]
users_file = open("./users.txt", "w")
for sample_user in sample_users:
username = sample_user[0]
password = sample_user[1]
users_file.write(username + ';' + password + '\n')
users_file.close()
Put that in a .py
file and run it. It should generate a file called users.txt
right on the same directory where the script is located. I suggest you take a look to the file (any text editor will do). You'll see it looks like this:
user1;password1
user2;password2
user3;password3
By the way, it's a much better practice taking advantage of the "autoclosing" features provided by Python's context managers. You could write that script as:
with open("./users.txt", "w") as users_file:
for sample_user in sample_users:
username = sample_user[0]
password = sample_user[1]
users_file.write(username + ';' + password + '\n')
See? No call to .close()
needed. If something happens while running the code, you will be assured that your file is closed after leaving the with
block (when the interpreter reaches the end of the with
block, a call to the File's special function __exit__
will be automatically run, and the file will be closed)
Ok... So we have a file with username;password\n
on each line. Let's use it.
For this part, you're gonna need to understand what the split
(to separate username and password using the semicolon) and rstrip
(to remove the newline symbol \n
at the end) methods of the str
objects do.
We're gonna need to "rebuild" two variables (username
and password
) from a line of text that has the shape username;password\n
. Then see if the username is found in the file, and if so, prompt the user for the password (and verify it's correct):
def loginFunction():
userEntry = ""
foundName = False
while userEntry == "":
userEntry = raw_input("Enter your username: ")
usersFile = open("users.txt", "r")
for line in usersFile:
print("This is the line I read:%s", line,)
# Read line by line instead of loading the whole file into memory
# In this case, it won't matter, but it's a good practice to avoid
# running out of memory if you have reaaaally large files
line_without_newline = line.rstrip('\n') # Remove ending \n
user_record = line_without_newline.split(';') # Separate into username and password (make the line a list again)
userName = user_record[0]
password = user_record[1]
# Might as well do userName, password = line_without_newline.split(';')
if userName == userEntry:
foundName = True
print("User found. Verifying password.")
passwordEntry = raw_input("Enter your password: ")
if passwordEntry == password:
print "Username and passwords are correct"
break
else:
print "incorrect"
userEntry = ""
if not foundName:
print("Username not recognised")
userEntry = ""
if __name__ == "__main__":
loginFunction()
I believe this should do what you want? Put a comment in the answer if you have other questions.
And have fun coding!
PS: So... How about pickle?
Pickle is a module that serializes Python objects into files in a "safer" and more automated way. If you wanted to use it, here's how (one way, at least):
Generating the passwords file:
import pickle
sample_users = [
["user1", "password1"],
["user2", "password2"],
["user3", "password3"]
]
with open('./users.txt', 'w') as f:
pickler = pickle.Pickler(f)
for sample_user in sample_users:
pickler.dump(sample_user)
As before, at this point I would recommend you take a look to how the file users.txt
looks like with a regular text editor. You'll see it's pretty different to the file before (the one with the username and password separated by semi colons). It's something like this:
(lp0
S'user1'
p1
aS'password1'
p2
a.(lp3
S'user2'
p4
aS'password2'
p5
a.(lp6
S'user3'
p7
aS'password3'
p8
a.%
Use the file:
import pickle
def loginFunction():
userEntry = ""
while userEntry == "":
userEntry = raw_input("Enter your username: ")
usersFile = open("users.txt", "r")
unpickler = pickle.Unpickler(usersFile)
while True:
try:
user_record = unpickler.load()
userName = user_record[0]
password = user_record[1]
if userName == userEntry:
print("User found. Verifying password.")
passwordEntry = raw_input("Enter your password: ")
if passwordEntry == password:
print "Username and passwords are correct"
else:
print "incorrect"
userEntry = ""
# Watch out for the indentation here!!
break # Break anyway if the username has been found
except EOFError:
# Oh oh... the call to `unpickler.load` broke
# because we reached the end of the file and
# there's nothing else to load...
print("Username not recognised")
userEntry = ""
break
if __name__ == "__main__":
loginFunction()
If you realize, when you do user_record = unpickler.load()
, you already get a 2 items Python list
in the user_record
variable. There's no need for you to transform from text onto list: the unpickler
has already done that for you. This is possible thanks to of all that "extra" information that was stored by picker.dump
into the file, which allows the unpickler
to "know" that the object that needs to be returned is a list.