+4 votes
in Programming Languages by (73.8k points)
I am writing a large amount of data to an output file. When I check the output file while writing is still in progress, I do not see all written data in the file. Is there any way to fix it so that I can see all the written data?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

The write() function is not flushing the output buffer to the disk and hence you do not see all the written data in the output file.

There flush() function can solve your issue. After the write() function, add the flush() function and you will see the content in the file.

E.g.

fo = open('opfile.txt', 'w')
for rec in recs:
    fo.write(rec)
    fo.flush()
fo.close()


...