0

So I just started learning python and tried a little project that consists of a login system. I want usernames and passwords to save into .txt files but they just won't and I can't figure out the error. The issue must be happening in the register dunction that I've defined because login works just fine with the username and password that I've introduced manually.

def register():
    user = input("Registration page\nUSERNAME:")
    users = open("users.txt", "r")
    if user in users:
        print("That user already exists!")
        login()
        users.close()
    else:
        pwrds = open("pass.txt", "a")
        users = open("users.txt", "a")
        users.write("\n" + user)
        p1 = input("PASSWORD:")
        p2 = input("CONFIRM PASSWORD:")
        if p1 == p2:
            pwrds.write("\n" + p1)
            login()
            users.close()
            pwrds.close()
        else:
            print("Failed password confirmation, restarting")
            register()
            users.close()
            pwrds.close()

Thank you in advance for the help!

tdelaney
  • 73,364
  • 6
  • 83
  • 116
nrivasm
  • 1
  • 3
  • `users = open("users.txt", "r")` users is now a file object -- not data within it. You need to read the file. – dawg May 11 '20 at 18:39
  • Add more of a description of what went wrong. Was there an exception? An unexpected output? An empty file? – tdelaney May 11 '20 at 18:41
  • @tdelaney There's no exception, the code runs okay. I expected the code to write the username to users.txt and the password to pass.txt and none of them write anything. – nrivasm May 11 '20 at 18:44
  • @dawg I'll try now, thank you for helping out! – nrivasm May 11 '20 at 18:45
  • @dawg - not quite. `in` will treat the file object as an iterator and read it line by line. But that line includes the `\n` so OP would need to do `if user + '\n' in users` – tdelaney May 11 '20 at 18:45
  • Just pointing this out, but if this is going to be for a website or something, this is very insecure. Consider encrypting/hashing the credentials and storing it on a server – Tabulate May 11 '20 at 18:51
  • I just tried what @dawg suggested and I've gotten the same result. `if user in users:` seems to be working fine, the problem isn't reading the file, the problem comes when I try to write in it. – nrivasm May 11 '20 at 18:54
  • @Tabulate I just started out using python, this is not for a website. It's just to practice very basic concepts :) – nrivasm May 11 '20 at 18:55
  • @tdelaney: Just tested that, and I don't think that is correct. If I set up a little test case (write some txt to a file) then open it only and use `in` for a word in that txt, it is `False`. If I add `.read()` to the end of the file object, it is `True`. Try it! – dawg May 11 '20 at 18:55
  • @NicolásdeRivas - user in users works for the first user but fails when you start putting newlines in there. – tdelaney May 11 '20 at 18:55
  • @tdelaney I see, so I should use .read() at the end right? – nrivasm May 11 '20 at 18:56
  • Okay, I added .read() at the end of that condition, it now looks like this: `if user in users.read():`. I still have the files not writing the passwords and usernames down though. – nrivasm May 11 '20 at 19:00
  • @NicolásdeRivas - No, you should not use read. In that case users "Ben" and "Benjamin" would both match "Ben". When you iterate the file you get lines including the line feed. Its much like `"Ben" in ["Ben\n", "Benjamin\n"]` which is false, but `"Ben" + "\n" in ["Ben\n", "Benjamin\n"]` is True. – tdelaney May 11 '20 at 19:01
  • @Dawg, `read()` isn't the right comparison here. Its more like checking if an item is in a list. `"Ben"` is not in `["Ben\n"]` – tdelaney May 11 '20 at 19:02
  • @tdelaney I see now. okay, changing that right now – nrivasm May 11 '20 at 19:03
  • @dawg - `in` iterates through the object til it finds the first match. So for files, it reads the lines. – tdelaney May 11 '20 at 19:04
  • @tdelaney: OK -- I changed the test and you are correct. I do disagree on the IDIOM (really should use `with` and read line by line, etc) but you did teach me something. Thanks! – dawg May 11 '20 at 19:04
  • @tdelaney I finished the code now, everything is working. I had a few problems with the recognition of passwords and usernames in the .txt files but managed to work them out, I wouldn't have realized about the \n at the end of some passwords if it wasn't for you though, so thanks, man! – nrivasm May 11 '20 at 19:59

1 Answers1

0

When you're checking the string inclusion (user in) in the file object (users), you always get False because each line is appended with the escaped new-line character '\n'.

Also, you're trying to open the file twice. There's no point for doing such of thing. You can open it passing the 'r+' parameter from the beginning.

Please note that the 'r+' method will put the initial position of the cursor at the beginning of the file, but since we're calling the read() method, then the cursor will be now set to the end of the file, allowing us to write the new user in the last position of it.

I would do this like follows:

def register():
    user = input("Registration page\nUSERNAME:")
    file = open("users.txt", "r+")
    users = file.read().splitlines() # considering you are saving the users one by one, in each line.
    if user in users:
        print("That user already exists!")
        users.close()
        login()
    else:
        pwrds = open("pass.txt", "a")
        users.write("\n" + user)
        users.close()
        p1 = input("PASSWORD:")
        p2 = input("CONFIRM PASSWORD:")
        if p1 == p2:
            pwrds.write("\n" + p1)
            pwrds.close()
            login()
        else:
            print("Failed password confirmation, restarting")
            pwrds.close()
            register()
revliscano
  • 2,227
  • 2
  • 12
  • 21
  • No problem, my friend. I advise you to check this awesome diagram -> https://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r/30566011#30566011 – revliscano May 11 '20 at 20:08
  • Great diagram, i didn't even know about the r+ and a+ options. – nrivasm May 11 '20 at 20:37