On 3/5/23, aapost <aap...@idontexist.club> wrote: > > If a file is still open, even if all the operations on the file have > ceased for a time, the tail of the written operation data does not get > flushed to the file until close is issued and the file closes cleanly.
This is normal behavior for buffered file I/O. There's no timer set to flush the buffer after operations have "ceased for a time". It automatically flushes only when the buffer is full or, for line buffering, when a newline is written. The default buffer size is based on the raw file object's _blksize attribute. If st_blksize can't be determined via fstat(), the default _blksize is 8 KiB. Here's an example on Linux. In this example, the buffer size is 4 KiB. >>> f = open('abc', 'w') >>> os.fstat(f.fileno()).st_blksize 4096 >>> f.buffer.raw._blksize 4096 >>> f.writelines(f'{i}\n' for i in range(50000)) >>> with open('abc') as g: g.readlines()[-1] ... '49626\n' >>> pre_flush_size = os.path.getsize('abc') >>> f.flush() >>> post_flush_size = os.path.getsize('abc') >>> post_flush_size - pre_flush_size 2238 Verify that this makes sense, based on what was left in the buffer prior to flushing: >>> remaining_lines = 50000 - 49626 - 1 >>> bytes_per_line = 6 >>> remaining_lines * bytes_per_line 2238 -- https://mail.python.org/mailman/listinfo/python-list