-1

So I'm trying to read in data from my arduino, however it seems that it only creates the file after my script has stopped executing. Either by stopping it manually or just running through the entire script.

Now my questions is mainly how can it already create the file during the while loop and append to it. And why is not working now?

import csv
import time

while True:
    row = ['1', '2', '3']

    with open('results.csv', 'a') as csvfile:
        csv_writer = csv.writer(csvfile)
        csv_writer.writerow(row)

    time.sleep(1)

The code blob above can be used to reproduce this behaviour. I would like to create results.csv while it is using the while loop and not after its execution.

Sil Bouwman
  • 101
  • 1
  • 8

1 Answers1

4
with open('results.csv', 'a') as csvfile:
    csv_writer = csv.writer(csvfile)
    csv_writer.writerow(row)

time.sleep(1)

Try closing the file once the rows are written.

This will flush all the data to disk.

If you want to force a flush to disk during the loop you'll have to call some type of flush method on the file (not sure what function this is in Python, but I think it's csvfile.flush()).

Here is some more information about file buffers and flushing the data to disk.

For example, the following code will flush the data to disk in each iteration but will also slow down the whole process because all the advantages of file buffering are gone:

while True:
    row = ['1', '2', '3']

    with open('results.csv', 'a') as csvfile:
        csv_writer = csv.writer(csvfile)
        csv_writer.writerow(row)
        csvfile.flush()

    time.sleep(1)
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • 1
    The file **gets closed** when you exit the `with` block, that's the point of using context managers, see for example https://stackoverflow.com/questions/31334061/file-read-using-open-vs-with-open for more info. Also, closing it flushes it, see https://stackoverflow.com/questions/2447143/does-close-imply-flush-in-python. So this won't make any difference. – Thierry Lathuille Apr 29 '20 at 07:21