0
 def login():
    contents = {}
    with open("pwdFile.txt") as f:
      for line in f:
        split = line.split("|")
        contents[split[0]]= ",".join(split[1:])
      if userName.get() in contents:
        print("Username exist")
        if contents[userName.get()] == pwd.get():
          print("logged in")


print(contents)
print(contents[userName.get()])
print(pwd.get())

This code is reading from a file to check if the stored password in the file matches the password that is inputted.Both passwords are identical however they do not match therefore the if contetns == pwd.get is not executed.I am using tkinter and getting pwd from an Entry() and passing it to the login() function.

Here is the text file its reading from: james|pwd

6
  • 2
    Why do you think they're identical? Commented Feb 18, 2018 at 1:42
  • because the output of contents[userName.get()] and pwd.get() which is printed at the bottom is identical Commented Feb 18, 2018 at 1:43
  • 3
    That's not what you're code proves. Your code proves that they look identical upon visual inspection. Commented Feb 18, 2018 at 1:44
  • would it be something to do with using \n for a new line after the password is written to the file? Commented Feb 18, 2018 at 1:47
  • 2
    I would suggest printing the repr of your strings, which will put quotation marks around them (making whitespace more apparent) and escape any special characters. Commented Feb 18, 2018 at 1:48

1 Answer 1

1

You have whitespaces at the end of each line, so they end up in pwd. Get rid of them using:

pwd.strip()

Note that it might not work well if it's possible any password contains leading or trailing whitespaces, but you shouldn't have plain text passwords anyways, right?

Sign up to request clarification or add additional context in comments.

3 Comments

thank! il try it now
its one of my first projects at uni building it up stage by stage. Password encryption is the next hurdle
Then you should be good if you ensure hashes you get for them don't use whitespaces.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.