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?

Add Comment
1 Answer(s)

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

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.