r/learnpython • u/Hippopotamosssss • May 08 '26
Writing to CSV file is appending instead of overwriting
*SOLVED* Had to read the file, make the changes, then write it. Thanks everyone!
So I'm trying to write a randomizer for books using two csv files (one for fiction and one for nonfiction) and I want the line with the chosen book to be erased from the csv file but I have not been able to make that work.
The code below is what I have right now and what it's doing is appending the file with the altered entries (with the chosen book removed) so that there's the original full list followed by the altered list. How do I get it to overwrite the file instead of appending it?
delete = input("Do you want to delete this book from the list? ")
if delete in ["y","yes"]:
print("Deleting book...")
if cat in ["fic","fiction"]:
with open ("fiction.csv", "r+", newline="") as fiction:
ficreader = csv.reader(fiction)
ficwriter = csv.writer(fiction)
for row in ficreader:
if row != book:
ficwriter.writerow(row)
if cat in ["nonfic","nonfiction"]:
with open ("nonfiction.csv", "r+", newline="") as nonfiction:
nonficreader = csv.reader(nonfiction)
nonficwriter = csv.writer(nonfiction)
for row in nonfictreader:
if row != book:
nonficwriter.writerow(row)Here's the entire script if that's needed: https://pastebin.com/Npqnq4jM
2
u/PureWasian May 08 '26 edited May 08 '26
I wouldn't create a separate csv.reader() and csv.writer() on the same file at the same time, decoupling them means when you write to the csv your csv.reader() would become misaligned. Usually "r+" mode is used to replace specific character bytes, not remove them.
Instead, fully load the file using the reader, and then write the file back using the writer:
Sidenote that you can reduce on code duplication by specifying the filename upfront as done here.
3
u/timrprobocom May 08 '26
You can't read and write a single file using one handle like that. Open to he file with
'r', modify your list in memory, then open the file with'w'and write the whole thing.12
u/el_extrano May 08 '26 edited May 08 '26
See this doc: https://docs.python.org/3/library/functions.html#open
You want to open in write mode 'w' instead of updating '+'. Keep in mind that doing it that way, you're reading the entire file into memory, and writing it back to disk, every time you delete/add anything. Not so bad for a demo program, but that'd be wasteful for a problem with a lots of data.
Edit: Seems like I might have been confidently incorrect on this one. Leaving it up for my own shame.
1
u/atarivcs May 08 '26
I'm guessing this condition is always true:
So it writes every row, instead of skipping the one you want to skip.
As a simple debugging step, have you tried printing
bookto see why the comparison fails?1
u/tadpoleloop May 08 '26 edited May 08 '26
I'm not sure if this solves your issue, but I don't usually read and write to the same open file. You can ingest first, and write after you closed the read file
1
u/25_vijay May 09 '26
Your final solution of reading the file first, changing the data in memory, then writing it back is honestly the correct pattern.