loginUsername = input("Cunso: ")
loginPassword = input(" JanganDibuka#08")
data=open('database.txt', 'r')
accounts = data.readlines()
for line in data:
accounts = line.split(",")
if (loginUsername == accounts[0] and loginPassword == accounts[1]):
print("LOGGED IN")
else:
print("Login SUCCES")
print(accounts)
How to check the username in text file or not and then ask for the password?
Asked 2 years, 7 months ago
Modified 2 years, 7 months ago
Viewed 3k times
2
loginUsername = input("Enter Username: ")
loginPassword = input("Enter PASSWORD: ")
data=open('database.txt', 'r')
accounts = data.readlines()
for line in data:
accounts = line.split(",")
if (loginUsername == accounts[0] and loginPassword == accounts[1]):
print("LOGGED IN")
else:
print("Login FAILED")
print(accounts)
I want to make a text login system, which will ask for the username first. After checking the text file which stored username and password, the system will ask for password. But I don't know how to read the first column (which is username, the structure of the text file is "username, password"). If i use readlines() and split(","). But there is "n" at the end of the password.
pythonjupyter-notebook
Share
Improve this question
Follow
asked Nov 8, 2022 at 12:56
Mike's user avatar
Mike
2111 silver badge22 bronze badges
What is: accounts = data.readlines()? Surely this exhausts the file. –
quamrana
CommentedNov 8, 2022 at 13:09
Welcome to stackoverflow Mike. If the answer you received solved your issue, you can mark it as "correct", by clicking on the check mark beside the answer, to toggle it from greyed out to filled in. –
Andreas Violaris
CommentedNov 8, 2022 at 21:04
Add a comment
Report this ad
2 Answers
Sorted by:
Highest score (default)
3
# You should always use CamelCase for class names and snake_case
# for everything else in Python as recommended in PEP8.
username = input("Cunso: ")
password = input("JanganDibuka#08: ")
# You can use a list to store the database's credentials.
credentials = []
# You can use context manager that will automatically
# close the file for you, when you are done with it.
with open("database.txt") as data:
for line in data:
line = line.strip("\n")
credentials.append(line.split(","))
authorized = False
for credential in credentials:
db_username = credential[0]
db_password = credential[1]
if username == db_username and password == db_password:
authorized = True
if authorized:
print("Login Succeeded.")
else:
print("Login Failed.")
mystring = "password\n"
print(mystring.rstrip())
>>> 'password'