2

I am making a pretend login system as a project. When the in_file variable is in the file I want it to print you are logged in. However, it doesn't do this since it doesn't think that the in_file variable is in the file. How do I fix this?

sample of the file

username/password

Matthew/06112004

James/Nemo20044!

account = input('do you want to sign up or login ? ')
file = open('login.txt', 'a+')

def log_in(filename):
    logging_in = True
    while logging_in:
        username = input('Username: ')
        password = input('Password: ')
        in_file = f'{username}/{password}'
        if in_file in filename.read():
            print('you are logged in')
            logging_in = False
        if in_file not in filename.read():
            print('user name or password are incorrect')

if account == 'login':
    log_in(file)
mr. bug
  • 309
  • 1
  • 11
  • 1
    You want to open the file in read mode, `file = open('login.txt', 'r')` and then your other problem is that you can only read a file once. Save the read call to a variable and then query the variable instead. – flakes Jul 02 '20 at 02:50
  • Also required watching!!! https://www.youtube.com/watch?v=8ZtInClXe1Q – flakes Jul 02 '20 at 02:51
  • Did my post answer your question? – Red Dec 31 '20 at 21:47

1 Answers1

0

You need to open the file with the 'r'(read) mode, the 'a' mode will only let you append to the file.

So change file = open('login.txt', 'a+') to file = open('login.txt', 'r').

Note that it is not a very good practice to open() and close() a file. Instead, use a context manager.

Red
  • 26,798
  • 7
  • 36
  • 58