35

I am trying to read and write on the same CSV file:

file1 = open(file.csv, 'rb')
file2 = open(file.csv, 'wb')
reader = csv.reader(file1)
writer = csv.writer(file2)
for row in reader:
   if row[2] == 'Test':
      writer.writerow( row[0], row[1], 'Somevalue')

My csv files are:

  • val1,2323,Notest
  • val2, 2323,Test

So basically if my row[2] value is Test I want to replace it with Some new value. The above code gives me empty CSV files.

wxs
  • 288
  • 3
  • 18
laspal
  • 553
  • 2
  • 6
  • 8

4 Answers4

23

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.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
19

You can't open a file in both read and write modes at once.

Your code could be modified as follows:-

# Do the reading
file1 = open(file.csv, 'rb')
reader = csv.reader(file1)
new_rows_list = []
for row in reader:
   if row[2] == 'Test':
      new_row = [row[0], row[1], 'Somevalue']
      new_rows_list.append(new_row)
file1.close()   # <---IMPORTANT

# Do the writing
file2 = open(file.csv, 'wb')
writer = csv.writer(file2)
writer.writerows(new_rows_list)
file2.close()

As Jason points out, if your CSV is too big for your memory, then you'll need to write to a different filename and then rename it. This will likely be a bit slower.

James Bradbury
  • 1,708
  • 1
  • 19
  • 31
  • You can/should use a context manager when opening files, i.e. `with open("file.csv", "rb")` - that way you won't have to remember to call `file1.close()`. – mecampbellsoup Dec 30 '22 at 17:51
4

If your csv file is not big enough(to explode the memory), read it all into memory and close the file before open it in write mode.

Or you should consider writing to a new file rather than the same one.

Jason
  • 807
  • 5
  • 15
0

It is not possible to open the same file in two different modes in python.You have to release one of the file pointers with file_name.close() before opening the file in another mode!

Aditya
  • 11
  • 1