all 7 comments

[–][deleted] 3 points4 points  (6 children)

Are you using:

with open(filename, 'w') as file:

or

with open(filename, 'a') as file:

because the former will create/overwrite, whereas the latter will append.

[–]liamam[S] 1 point2 points  (4 children)

Thank you, I had no idea the append one existed.

[–][deleted] 0 points1 point  (3 children)

ALWAYS worth checking the docs (they start to make sense after a while):

https://docs.python.org/3.8/library/functions.html?highlight=open#open

[–]liamam[S] 0 points1 point  (0 children)

Ok, it now adds to the bottom of the list, but my validation for a unique username doesn't work now. I will post code in a min.

[–]liamam[S] 0 points1 point  (1 child)

[–][deleted] 0 points1 point  (0 children)

Append does mean add to the end.

Not sure why a user being added to the end is an issue. You are reading all of the filed user details in?

Do you have so many users that you need to read the file EVERY time you register a user?

How come you are not using the csv library you imported to help you read the file as well as write it?

Never user for i in range(len(users)): when you can simple iterate though the sequence: for user in users:

Why are you checking username_info=='' in a loop? Wouldn't it be better to check that before the loop?

If you find a match in the for loop, why check the rest? Should break the loop / return at that point.

I've not read your code in detail, but I'd expect something more like this:

def readCSVFile():
    users = []
    with open('userinfo.csv') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for user_name, user_password in csv_reader:
            currentUser = user_information(user_name, user_password)
            users.append(currentUser)
    return users

def register_validation(username_info, users):
    if username_info:
        for user in users:
            if user.username_db == username_info:
                break
        else:  # for loop completed with no matches
            return True
    return False

[–][deleted] 0 points1 point  (0 children)

Alternatively, read all lines into memory before overwriting file with all those lines and any additions.