0
def runlogin():
    if choice1 == "yes":    
        username=input("Enter your username")
        password=input("Enter your password")

        file = open("logins.txt","r")
        if (file.readline()) == (username+":"+password):
            print("Access Granted")
        else:
            print("Access Denied")


print("Welcome")
choice1=input("Have you already created an account? ('yes' or 'no')")
choice1=choice1.lower()

if choice1 == "no":
    createusername=input("Please enter a username")
    createpassword=input("Please enter a password")
    filefile=open("logins.txt","a")
    filefile.write(createusername+":"+createpassword)
    print("Account created, redirecting to login page")
    filefile.close()


if choice1 == "yes":    
    runlogin()

When the users chooses "no", they are then prompted to create an account. My issue is, when they input their details and then create the account, nothing is added to the text file

The text file is formatted like "username:password"

No syntax error occurs

  • 2
    Side note: you should not be opening and closing files manually, use the context manager `with` that manages it for you. – Paritosh Singh Sep 26 '19 at 16:02
  • 1
    You only ever check the first user in the file. The append works fine. – Mad Physicist Sep 26 '19 at 16:04
  • 1
    @MadPhysicist: Actually, it's worse than that. They never write newlines, so if there is a second user in the file, the `readline` gets both, and (barring entering user names or passwords with colons in them) even the first user will never match, because it will be comparing against the concatenation of *all* of the users and their passwords. – ShadowRanger Sep 26 '19 at 16:19
  • 1
    @Noaaaa: How do you know nothing is added to the text file? Are you looking at the file itself, or are you just guessing based on the inability to authenticate? If it's by looking at the file itself, are you sure the working directory (different from the *script* directory) is what you think it is? It's possible (and for a script launched via `PATH` lookup, rather likely) the working directory is different, and all your login info is being written to `logins.txt` in a directory (more than one if each launch is from a different working directory) you don't expect. – ShadowRanger Sep 26 '19 at 16:21
  • Fixing the working directory issue could be done by providing an absolute path to `logins.txt`, or a relative path rooted in `sys.exec_prefix` (making it relative to Python itself) or `__file__` (making it relative to the script location). – ShadowRanger Sep 26 '19 at 16:25
  • Possible duplicate question: [How do you append to a file in Python?](https://stackoverflow.com/q/4706499/3345375) – jkdev Sep 26 '19 at 17:47
  • @jkdev: The OP clearly already knows how to do so; they're already opening in append mode (`'a'`). They have multiple problems, but actually knowing how to append to a file is not one of them; nothing in that question would help them. – ShadowRanger Sep 26 '19 at 18:28
  • @ShadowRanger Should the question title should be edited to include more details? The current title "How do I successfully 'append' a text file and add to it" isn't sufficiently descriptive of the actual issue. – jkdev Sep 26 '19 at 21:10

0 Answers0