read and write on same csv file

You should use different output file name. Even if you want the name to be the same, you should use some temporary name and finally rename file.

When you open file in ‘w’ (or ‘wb’) mode this file is “cleared” — whole file content disappears. Python documentation for open() says:

… ‘w’ for only writing (an existing file with the same name will be erased), …

So your file is erased before csv functions start parsing it.

Leave a Comment