Reputation: 103
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?
Upvotes: 2
Views: 1545
Reputation: 745
Use the below code
with open('employee data.csv', 'a')
parameter 'a' instructs to append.
Upvotes: 0
Reputation: 1048
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
Upvotes: 3