I have a code where I have an email and a password in a text file in a list, formatted like this: ["[email protected]","Password1"],["[email protected]","Password2].
My code reads from the text file and compares it with a user input to see if it is present in the text file. For some reason when I input the first email "[email protected]" it reads that from the text file but not "Password1", however it reads "[email protected]" correctly as well as "Password2" correctly. I cant find any way to get around this and fix this? No error is received so I'm not sure where its going wrong. Here is my code:
def Login():
with open("logindata.txt","r") as file:
for line in file:
line=line.split(",")
logindata.append([line[0],line[1].rstrip()])
print(logindata)
found=False
while found == False:
email=input("Enter email:")
password=input("Enter password:")
with open("logindata.txt","r")as file:
for line in file:
line = line.split(",")
if email in line:
print("Correct email")
if password in line:
print("Correct Password")
found=True
if __name__ == "__main__":
Login()
if the code is run and you try the inputs I gave at the start then only the second email and password combination works. Here is what the text file looks like text file format
Appending:
username=input("Enter a new email")
password=input("Enter a password")
with open('logindata.txt','a')as file:
line='\n'+ username + ',' + password
file.write(line)
print("user added")
passwordis not in the line,password\nis.csv.writer()to write the file. Each time you use thewriterow()method on that it'll add the newline for you. Otherwise, add it manually when writing;fileobj.write('{},{}\n'.format(userid, password)), for example.