-1

I am trying to write to a csv file that already exists and for the first entry it works fine but when adding a second one it simply rewrites the first entry.

code:

def start(names, estimated_time, due_date):


data = [names, estimated_time, due_date]

if os.path.isfile("C:\\Users\\Drago\\TaskManager\\tasks.csv"):
    with open("C:\\Users\\Drago\\TaskManager\\tasks.csv", 'w', newline='') as myfile:
        wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
        wr.writerow(data)

Any help would be appreciated thank you for your time :)

Equinox
  • 120
  • 2
  • 8

1 Answers1

1

When you open a file, using 'w' will truncate the file. Open with 'a' to append to a file. Just remember not to keep adding the header when there is already content in the file.

Update: I've never used the csv module before, but I would think if you are calling this multiple times in your program, perhaps you should just open the writer once, and add to it and close it at the end.

Ben Y
  • 913
  • 6
  • 18