admin管理员组文章数量:1315778
Here's the code
login = open("login.txt", "a+")
passw = open("passw.txt", "a+")
state = 0
print("What do you want to do?")
print("1. Log in")
print("2. Register")
dec = int(input())
if dec == 1:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
if log == login.read() and pas == passw.read():
print("You are logged in")
state = 1
login.close()
passw.close()
else:
print("Incorrect login or password")
state = 0
login.close()
passw.close()
elif dec == 2:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
login.write(log)
passw.write(pas)
print("You are registered")
state = 1
login.close()
passw.close()
if state == 1:
print("What do you want to do?")
print("1. Read the file")
print("2. Write to the file")
print("3. Log out")
dec = int(input())
if dec == 1:
file = open("supasceret.txt", "r")
print(file.read())
file.close()
elif dec == 2:
file = open("supasecret.txt", "a+")
print("Enter the text:")
text = input()
file.write(text)
file.close()
elif dec == 3:
state = 0
print("You are logged out")
when i tried to add another function between lines 41-42 its suddenly stopped working even reverting the code and running it now still doesn't work and now it constantly says "Incorrect login or password". I even cleared the files and registered fromt he beggining still the same result
I was logging in and was expecting to receive the message "You are logged in"
Here's the code
login = open("login.txt", "a+")
passw = open("passw.txt", "a+")
state = 0
print("What do you want to do?")
print("1. Log in")
print("2. Register")
dec = int(input())
if dec == 1:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
if log == login.read() and pas == passw.read():
print("You are logged in")
state = 1
login.close()
passw.close()
else:
print("Incorrect login or password")
state = 0
login.close()
passw.close()
elif dec == 2:
print("Enter your login:")
log = input()
print("Enter your password:")
pas = input()
login.write(log)
passw.write(pas)
print("You are registered")
state = 1
login.close()
passw.close()
if state == 1:
print("What do you want to do?")
print("1. Read the file")
print("2. Write to the file")
print("3. Log out")
dec = int(input())
if dec == 1:
file = open("supasceret.txt", "r")
print(file.read())
file.close()
elif dec == 2:
file = open("supasecret.txt", "a+")
print("Enter the text:")
text = input()
file.write(text)
file.close()
elif dec == 3:
state = 0
print("You are logged out")
when i tried to add another function between lines 41-42 its suddenly stopped working even reverting the code and running it now still doesn't work and now it constantly says "Incorrect login or password". I even cleared the files and registered fromt he beggining still the same result
I was logging in and was expecting to receive the message "You are logged in"
Share Improve this question edited Jan 30 at 0:15 InSync 10.9k4 gold badges17 silver badges56 bronze badges asked Jan 30 at 0:01 KosteQKosteQ 111 silver badge1 bronze badge 6 | Show 1 more comment2 Answers
Reset to default 1Instead of opening files in a+ mode, you should use r+ mode to properly handle both reading and writing. Before performing a read operation, use seek(0) to reset the file pointer to the beginning; otherwise, read() may return an empty value. When comparing the read data, use .strip() to remove unnecessary spaces. Additionally, during the registration process, clearing the file content with truncate() before writing new entries will prevent old data from persisting. These changes will ensure that the login and registration functions work correctly without errors.
Here all recommendations that were mentioned in comments:
LOGIN_FILE = "login.txt"
PASS_FILE = "passw.txt"
state = 0
print("What do you want to do?")
print("1. Log in")
print("2. Register")
dec = int(input("> "))
if dec == 1:
log = input("Enter your login: ")
pas = input("Enter your password: ")
with open(LOGIN_FILE, "r") as login:
login.seek(0)
# strip() removes `\n` (newline sybmbol) when it reads
login = login.read().strip()
with open(PASS_FILE, "r") as passw:
passw.seek(0)
passw = passw.read().strip()
if log == login and pas == passw:
print("You are logged in")
state = 1
else:
print("Incorrect login or password")
state = 0
elif dec == 2:
log = input("Enter your login: ")
pas = input("Enter your password: ")
with open(LOGIN_FILE, "w") as login:
login.truncate(0) # clear file content
login.write(log)
with open(PASS_FILE, "w") as passw:
passw.truncate(0)
passw.write(pas)
print("You are registered")
state = 1
if state == 1:
print("What do you want to do?")
print("1. Read the file")
print("2. Write to the file")
print("3. Log out")
dec = int(input())
if dec == 1:
file = open("supasceret.txt", "r")
print(file.read())
file.close()
elif dec == 2:
file = open("supasecret.txt", "a+")
print("Enter the text:")
text = input()
file.write(text)
file.close()
elif dec == 3:
state = 0
print("You are logged out")
Let's your third block of code (where actions with supasceret.txt
) be your "homework". And Try to use more often debugger (VS code, Pycharm, pdb, ipdb and etc). and google.
P.S. I'm not pro at python and at programming, and if somebody think that this code could be better, feel free do it.
本文标签:
版权声明:本文标题:I was experimenting with file reading in python and it just stopped working after i tried to add something - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741991910a2409273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
a+
mode, the input pointer is positioned at the end of the file. So your.read()
call returns an empty string because the file is already at the end of its contents. – John Gordon Commented Jan 30 at 0:02with
statement. – Barmar Commented Jan 30 at 0:10