Michael Stahl
Michael Stahl

Reputation: 248

csv writer leaves empty output file when the python script crashes

I have a script that runs some analysis on files, then write the results to a CSV file. In pseudo code:

while (there are still files to analyze): 
  do_analysis
  writer.writerow({... lots of columns names and the analysis results data... })

If the script completes, the data is indeed in the csv file. But if the script crashes - the files are empty. But the writer.writerow() line is called after each file analysis is complete, so I was expecting to get data in the csv file for all files that were analysed before the crash.

What am I doing wrong?

Upvotes: 0

Views: 559

Answers (1)

HackLab
HackLab

Reputation: 539

writerow does not instantly write to the file, most of the writing is done when closing the file. You can enforce the writing to the file with

file.flush()

do this whenever you want to save your writings to the file.

For further information https://www.tutorialspoint.com/python/file_flush.htm

Upvotes: 1

Related Questions