为什么文件.读取返回空行?



我正在编写一个登录或注册帐户的程序。但是,当我试图读取文本文件以检查用户名和密码时,由于某种原因,file.read()没有返回任何内容。下面是代码:

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!')

这个程序将总是给我'password incorrect'消息,因为open('AccountDatabase.txt', 'r'):总是返回一个空行。

下面是我的完整代码:
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()

代替

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

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

,这里我们在append模式下打开文件,在read模式下,我们读取文件,并确保用户还没有存在,并附加新用户。如果用户不在数据库中,则为密码

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

最新更新