Jump to content

So I am trying to make a code that will allow user names and passwords to be entered in a file separated by a comma and a space then when the user enters their username and password in to be authenticated. But if it doesn't not exist then the the username is added to the system along with a typed in password.

 

Also once they are authenticated, the username is assign to a variable. This will then repeat for a second user.

 

My current code is:

 

import csv
logIn = False

correct_login_found = False

if answer == 'yes':
    csvfile = open('users.txt')
    reader = csv.reader(csvfile)
    username = input("Username:")
    password = input("Password:")

    for row in reader:
        if row[0] == username and row[1] == password:
            correct_login_found = True
            break

if correct_login_found == False:
    print("Bad login details")
else:
    print("You are now logged in!")

My formatting within my file is:

username, password

Thanks!

Link to comment
https://linustechtips.com/topic/1021707-python-csv-file-authentication-help/
Share on other sites

Link to post
Share on other sites

10 minutes ago, KeyboardShortcuts said:

So I am trying to make a code that will allow user names and passwords to be entered in a file separated by a comma and a space then when the user enters their username and password in to be authenticated. But if it doesn't not exist then the the username is added to the system along with a typed in password.

 

Also once they are authenticated, the username is assign to a variable. This will then repeat for a second user.

 

My current code is:

 


import csv
answer = input("Welcome to knowledge quiz! Do you have an account? yes or no")
logIn = False

correct_login_found = False

if answer == 'yes':
    csvfile = open('users.txt')
    reader = csv.reader(csvfile)
    username = input("Username:")
    password = input("Password:")

    for row in reader:
        if row[0] == username and row[1] == password:
            correct_login_found = True
            break

if correct_login_found == False:
    print("Bad login details")
else:
    print("You are now logged in!")

 

Thanks!

If you are going to save passwords like this (which I wouldn't recommend in any setting other than learning to code) you could take a loot at dictionaries that you save with json. That way you can just dump and load the file and when you want to search a username you just check the dictionary in stead of looping through the CSV file.

 

If you do it with CSV it would still be easiest to just load in all the usernames and passwords, find the password and otherwise add one to the list and replace the whole file.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to post
Share on other sites

1 minute ago, martward said:

If you are going to save passwords like this (which I wouldn't recommend in any setting other than learning to code) you could take a loot at dictionaries that you save with json. That way you can just dump and load the file and when you want to search a username you just check the dictionary in stead of looping through the CSV file.

 

If you do it with CSV it would still be easiest to just load in all the usernames and passwords, find the password and otherwise add one to the list and replace the whole file.

How could I do that for my situation?

Link to post
Share on other sites

Just now, KeyboardShortcuts said:

How could I do that for my situation?

The dictionary thing?

So basically you make a dictionary with keys being usernames and values being passwords. Then you can use json.dumps to dump it to a file. Next time you want it you can load the dictionary with json.loads and you'll have your dictionary back. 

 

def load_dict(file_name):

    with open('users.json', 'r') as in_file:

       users_dict = json.loads(in_file)

   return users_dict

 

def dump_dict(file_name):

    with open('users.json', 'r') as in_file:

       users_dict = json.dumps(in_file)

 

This is pseudo code but it would look something like this.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to post
Share on other sites

1 minute ago, martward said:

The dictionary thing?

So basically you make a dictionary with keys being usernames and values being passwords. Then you can use json.dumps to dump it to a file. Next time you want it you can load the dictionary with json.loads and you'll have your dictionary back. 

 

def load_dict(file_name):

    with open('users.json', 'r') as in_file:

       users_dict = json.loads(in_file)

   return users_dict

 

def dump_dict(file_name):

    with open('users.json', 'r') as in_file:

       users_dict = json.dumps(in_file)

 

This is pseudo code but it would look something like this.

Okay thanks so how could i ask the user then validate their username and password then assign to a username for two users?

Link to post
Share on other sites

2 minutes ago, KeyboardShortcuts said:

Okay thanks so how could i ask the user then validate their username and password then assign to a username for two users?

Well usually you don't allow users with the same usernames.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to post
Share on other sites

 

 

Are you guys in the same college class? Maybe you can pm that op and you can work together on this problem. I also posted code using a hash as@martwardssuggested

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

2 hours ago, vorticalbox said:

 

 

Are you guys in the same college class? Maybe you can pm that op and you can work together on this problem. I also posted code using a hash as@martwardssuggested

Yeah, just when I asked him, he didn't really help me as he didn't know what he was doing. So I tried to simplify it to simple goals.

 

Sorry.

Link to post
Share on other sites

17 minutes ago, KeyboardShortcuts said:

Yeah, just when I asked him, he didn't really help me as he didn't know what he was doing. So I tried to simplify it to simple goals.

 

Sorry.

No need to be sorry. You should however not store logins in a CSV but rather in a hash.

 

Even better would be to use a database.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

9 hours ago, KeyboardShortcuts said:

How can I do that?

 I can not express the importance of reading the documentation.

 

If there is something you need to know then that's the place to go.

 

I would look into a module called tinydb 

https://github.com/msiemens/tinydb/blob/master/README.rst

 

That will give you the insert, find and update just like a real database but stores in a JSON file so you don't have the issue of setting up a real database.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

8 hours ago, vorticalbox said:

 I can not express the importance of reading the documentation.

 

If there is something you need to know then that's the place to go.

 

I would look into a module called tinydb 

https://github.com/msiemens/tinydb/blob/master/README.rst

 

That will give you the insert, find and update just like a real database but stores in a JSON file so you don't have the issue of setting up a real database.

Cool, I'll look into it

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×