Unable to write new account information to a csv file

63 Views Asked by At

Working on making a password manager for a school project and I'm having trouble trying to add new account information to my password_manager.csv file. The information is being pulled from the other file when entered and it's printing successfully but when I try writing it to the csv file I get errors.

 import csv

 def createNewAccount(appName,userEmail,userName,userPassword):

    data = [appName,userEmail,userName,userPassword]

    print(data)
    
    with open('password_manager.csv','a') as csv_file:
        csv_writer = csv_file.write(csv_file, delimiter = '\t')
        csv_file.writerow(data)  
1

There are 1 best solutions below

0
On

I can' test this because you didn't say exactly what errors you are getting (which you should always do when asking questions here). That said, it looks to me like you're not using the csv module correctly — see the csv.writer documentation which contains example code.

The problem I see with your code are corrected in what's below:

import csv


def createNewAccount(appName, userEmail, userName, userPassword):

    data = [appName, userEmail, userName, userPassword]
    print(data)

    with open('password_manager.csv', 'a', newline='') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter='\t')
        csv_writer.writerow(data)


if __name__ == '__main__':
    createNewAccount('appName1', 'userEmail1', 'userName1', 'userPassword1')
    createNewAccount('appName2', 'userEmail2', 'userName2', 'userPassword2')