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()