I need help, I am completely lost. I am making a register and login system as a school project,So I want to turn this textfile into a list and then make the two different columns (x, y) as (username, password) so I'll be able to login even after I restarted the program, but I do not know how
#python, textfile into list
32 messages · Page 1 of 1 (latest)
What problem are you having exactly?
why would you use a text file for this? it would be easier to use something like json if this is just a school project
It is a bit hard for me to explain as eng is not my first language but I'll try here,
This is the original code, it works but everytime I restart my program, I can't use the login details from the text file that I registered from previous sessions, only the recent session
my lecturer said to use either textfile, list or dictionary
or/and
I mean it looks like you're on the right track. The dictionary data will be of the format {"user": "password"}
You probably want to return it from your function and do something with it?
def register()
... # your stuff here
return data
data = register()
print(data)
technically json is a dictionary (i guess?)
A text file could work considering that it's just multiple entries of usernames and passwords, assuming the usernames are the unique key
it could, i just feel like it is a bit fidily 🤷♂️ but i have never tried to use it apart from when i had a little bit of knowledge on py and was following tutorials mostly
Json isn't always better :))
yes but is there a way to store the data from the textfile into the dictionary right away
ok hold on
You need to call that function everytime you run your program
When a python program stops it forgets everything that it had in memory
the red one is the one I just registered in the current session, while the one not highlighted are username and passwords I registered in previous sessions, I can only use the one in red to log in
You need to load the entire file every time you run your program
You have the function that does that above
yes is there a way to make it so that the program reads the textfile, stores all of the username and password instantly wether into a list/dictionary so I can use all of the username and passwords including from the previous sessions
That's what your register function does. If you're asking if there is a way for python to magically know how to do this then no, you have to write code to do it. Which you've done
Unless there's more code in your register function than you've shared that's confusing matters
this is the whole register() function
is there a way to not use dictionary but makes it function the same
Oh 😅
Well when you try to login you need to be using the entirety of the contents of the text file. You could read it again or have your register function keep an update copy of user and passw in memory
What I'd do is on the program starting read the file, using the first part of what you have in register and store that in data outside the function, by returning it like I showed above. Then remove that part from register
Then your register function should do everything you have now including appending to the file. But it should also update data with the username and password.
Then when you try to login (I'm not sure where you are doing that). You'd use data which contains all the users and passwords from the text file, including any that you've registered while the program was running
def load_data():
# load the file here with the code you have now
data = dict(zip(user, passw))
return data
def register(data):
# everything you have now for adding user to the file but also add the user to data dictionary
# You might need to modify it a bit to work with the data dictionary rather than the separate users and passw lists
data[username] = password
return data
def login(data):
username = input("Username: ")
password = input("Password: ")
if data.get(username) == password:
print("Logged in!")
if __name__ == "__main__":
#load data
data = load_data()
#register a new user
data = register(data)
#login
login(data)
Something like that
what do you mean by modifying it
Well instead of if username in user you'd need to change it to the dictionary