2

I'm a beginner with python so you can think that am i asking a stupid question, but i really can't understand why mine function is removing previous data in csvfile instead of adding new in to this file. That's how it looks like:

def writein():
    with open('employee data.csv', 'w') as employeeData:
        writeby = csv.writer(employeeData, delimiter = ',')
        writeNEN = input('Add name of new emp: \n')
        writeNESN = input('Add surname of new emp: \n')
        writeNEP = input('Add position of new emp: \n')
        writeNEBD = input('Add date of birth of new emp: \n')
        writeby.writerow([writeNEN, writeNESN, writeNEP, writeNEBD])

Have you guys any idea how to make program adding new data instead of replacing it?

2 Answers 2

3

When you open a file in write-mode (i.e. open('file.txt', 'w')), the contents are cleared before things are added. The fix to your problem would be to open the csv file in append-mode:

def writein():
    with open('employee data.csv', 'a') as employeeData:
                             #      ^
                             # a for append
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, for a strange reason there is nothing about append here: docs.python.org/3.7/library/csv.html
I think you'll find this link useful.
0

Use the below code with open('employee data.csv', 'a')

parameter 'a' instructs to append.

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.