0

I have the following situation: I'm writing a csv text file and I can not lose the data written on it.

However, in situations like the following, I lose everything I wrote to the fourth iteration.

import csv
import time

file_reference = open('result.csv', 'w', newline='')
file_csv = csv.writer(file_reference, delimiter=';', quoting=csv.QUOTE_MINIMAL)

for i in range(50):
    file_csv.writerow([i])
    time.sleep(1)
    print('...')

    if i == 4:
        raise ValueError('foo')

file_reference.close()

How could I write to disk the data written in the text file for each execution writerow?

macabeus
  • 4,156
  • 5
  • 37
  • 66
  • 1
    What if you [configure the buffer size](http://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file) for the file (to turn off buffering)? – Dan Mašek May 13 '16 at 01:01
  • @DanMašek Resolved. I did not know this parameter. I used `buffering = 1`. Thank you. If possible, answer the question, so I mark it as resolved. – macabeus May 13 '16 at 01:08
  • do a file flush every time after writerow(). it will slow down your application and won't guarantee a sync, which actually write back to disk, but may help you somewhat – DAG May 13 '16 at 04:53
  • if you want a real sync, use os.fsync – DAG May 13 '16 at 04:54

1 Answers1

0

Why are you raising a ValueError inside the for loop? Wouldn't it be much easier to just break out of the for loop flush/close the file and then raise the ValueError? However, if you have to have raise the exception inside the for loop why not use a context manager?

import csv
import time

with open('result.csv', 'w') as file_reference:
    file_csv = csv.writer(file_reference, delimiter=';', quoting=csv.QUOTE_MINIMAL)

    for i in range(50):
        file_csv.writerow([i])
        time.sleep(1)
        print('...')

        if i == 4:
            raise ValueError('foo')

    file_reference.close()
aquil.abdullah
  • 3,059
  • 3
  • 21
  • 40