I was trying to make some sort of login system, I have it so that if a username and password are in test.txt (there is multiple ones) it should let you login, I haven’t even passed the step of verifying if the username and password are in the txt file and its destroying me, I don’t know how to do it and I tried for hours, I made it so “if you find this username in the text file, give me the line number and check if the password this password is in the same line of the username ,(I used split (‘,’)) , if both email and password entered are existent in the txt file and in the same line then..(didn’t do that yet).
so it is confusing me, lots of errors, if no errors then it isn’t working like I intended, here is my spaghetti code
def test(): with open('test.txt', 'r') as f: for num, line in enumerate(f,1): username = line.split(',') if username in num: if username == q1: print("found user in line: " + num) Line = num password = line.split(',') if password in Line: if password == q2: print("found pass in line: " + num)
can someone help me fix this and explain to me how storing data in .txt files work and how to retrieve them? YouTube and google didn’t help much really, if you can suggest a video that will be cool too, because I’m confused at this point, all I have left is to try MongoDB because it has functions to retrieve data and store it already built in
but as its local on my pc not on the internet, I don’t think I will need MongoDB, so that will be an overkill for a local test
Advertisement
Answer
With json, you can get it done this way:
JSON File
{ "user1":{"password":"123456"}, "user2":{"password": "abcde"} }
Python
import json def test(username, password): with open("answer.json", "r") as read_it: data = json.load(read_it) if data[username][password] == '123456': print('User found!') else: print('User or password doesn't exist') test('user1', 'password')