0

I am working on a project which I figured would need to use something like a CSV document for storing data. But I ran into a problem, I need python to write the variable into the next line every time instead of the first line (this is overwriting the existing data in that column). Could anyone please help me to solve my problem?

Here is my code:

import csv
tag=input('Enter label for the new profile: ')
pwd=input('Type password for the new profile: ')
b=open('database.csv', 'w',)
a=csv.writer(b)
a.writerow([tag,pwd])
b.close()

Thanks in advance.

1 Answer 1

2

You want to use 'append' mode for your write.

Change your 'w' mode in your open 'a'.

Change this:

b=open('database.csv', 'w',)

to this:

b=open('database.csv', 'a',)

Read the documentation here on the different modes

'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.