Why does file.read return a blank line?

62 views Asked by At

I'm making a program to login or register an account. But when I tried to read a text file to check for the username and password, file.read() returned nothing for some reason. Here is the code:

def login_incorrect():
    Label(loginPage, text='Username or password incorrect.').place(x=120, y=120)
def LoginToAccount():
    with open('AccountDatabase.txt', 'r'):
        if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():
            login_incorrect()
        else:
            print('Logged in!')

This program will always give me the 'password incorrect' message, because open('AccountDatabase.txt', 'r'): always returns a blank line.

Here is my full code:

from tkinter import *
import time
root = Tk()
root.title("Account Signup")
DarkBlue = "#2460A7"
LightBlue = "#B3C7D6"
root.geometry('350x230')
LoggedIn = False
Menu = Frame()
loginPage = Frame()
registerPage = Frame()
for AllFrames in (Menu, loginPage, registerPage):
    AllFrames.place(relwidth=1, relheight=1)  # w/h relative to size of master
    AllFrames.configure(bg=LightBlue)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
def show_frame(frame):
    frame.tkraise()
show_frame(Menu)

def login_incorrect():
    Label(loginPage, text='Username or password incorrect.').place(x=120, y=120)
def LoginToAccount():
    with open('AccountDatabase.txt', 'r'):
        if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():
            login_incorrect()
        else:
            print('Logged in!')
def CreateNewAccount():
    print('create new account')
    while True:  # This loop will run as long as the new account hasn't been created.
        with open('AccountDatabase.txt'):
            if len(newUsernameE.get()) < 4:
                lenError = Label(text='Username must be 4 characters or more.')
                print('4')
                lenError.place(x=120, y=120)
            if newUsernameE.get() + "." in open('AccountDatabase.txt').read():
                print('username taken')
                # newUsername = input("Sorry, this username is already taken. Please choose another username:")
                continue
            if newUsernameE.get() + '.' not in open('AccountDatabase.txt').read():
                print('create pass')
                AccountDatabase.write(newUsernameE.get() + "." + newPasswordE.get() + "\n")
                break

# ============= Menu Page =========

menuTitle = Label(Menu, text="Menu", font=("Arial", 25), bg=LightBlue)
menuTitle.place(x=130, y=25)

loginMenuButton = Button(Menu, width=25, text="Login", command=lambda: show_frame(loginPage))
loginMenuButton.place(x=85, y=85)

registerMenuButton = Button(Menu, width=25, text="Register", command=lambda: show_frame(registerPage))
registerMenuButton.place(x=85, y=115)

# ======== Login Page ===========

loginUsernameL = Label(loginPage, text='Username')
loginUsernameL.place(x=30, y=60)
loginUsernameE = Entry(loginPage)
loginUsernameE.place(x=120, y=60)
loginPasswordL = Label(loginPage, text='Password')
loginPasswordL.place(x=30, y=90)
loginPasswordE = Entry(loginPage)
loginPasswordE.place(x=120, y=90)
backButton1 = Button(loginPage, text='Back', command=lambda: show_frame(Menu))
backButton1.place(x=0, y=0)
loginButton = Button(loginPage, text='Login', width=20, command=LoginToAccount)
loginButton.place(x=100, y=150)

# ======== Register Page ===========

newUsernameL = Label(registerPage, text='New Username')
newUsernameL.place(x=43, y=60)
newUsernameE = Entry(registerPage)
newUsernameE.place(x=140, y=60)
newPasswordL = Label(registerPage, text='New Password')
newPasswordL.place(x=45, y=90)
newPasswordE = Entry(registerPage)
newPasswordE.place(x=140, y=90)
confirmPasswordL = Label(registerPage, text='Confirm Password')
confirmPasswordL.place(x=25, y=120)
confirmPasswordE = Entry(registerPage)
confirmPasswordE.place(x=140, y=120)
backButton2 = Button(registerPage, text='Back', command=lambda: show_frame(Menu))
backButton2.place(x=0, y=0)
registerButton = Button(registerPage, text='Login', width=20, command=CreateNewAccount)
registerButton.place(x=100, y=180)

root.mainloop()

2

There are 2 answers

1
Nikappa_ On

replace this

with open('AccountDatabase.txt', 'r'):
    if loginUsernameE.get() + '.' + loginPasswordE.get() not in open('AccountDatabase.txt').read():

to

with open('AccountDatabase.txt', 'r') as f:
    if loginUsernameE.get() + '.' + loginPasswordE.get() not in f.read():
        login_incorrect()
    else:
        print('Logged in!')
0
Hanna On
def LoginToAccount():
    with open('AccountDatabase.txt', 'r') as f:
        if loginUsernameE.get() + '.' + loginPasswordE.get() not in f.read():
            login_incorrect()
        else:
            print('Logged in!')

and here we open the file in append and in read mode we read the file and make sure that the user isn't already there and append the new user.password if the user isn't in the database

def CreateNewAccount():
    print('create new account')
    while True:  # This loop will run as long as the new account hasn't been created.
        with open('AccountDatabase.txt', 'r') as fr, open('AccountDatabase.txt', 'a') as fa:
            if len(newUsernameE.get()) < 4:
                lenError = Label(text='Username must be 4 characters or more.')
                print('4')
                lenError.place(x=120, y=120)
            if newUsernameE.get() + "." in fr.read():
                print('username taken')
                # newUsername = input("Sorry, this username is already taken. Please choose another username:")
                continue
            else:
                print('create pass')
                fa.write(newUsernameE.get() + "." + newPasswordE.get() + "\n")
                break