all Python 3ioterm
Buffering
buffering
Buffered I/O collects the data in memory before writing it out. flush() forces the buffer out.
Syntax
open(file, buffering=-1) # -1 авто, 0 без буфера (только binary), 1 строчный
stream.flush()
buffering=0 is allowed in binary mode only — in text mode open() raises ValueError, while line buffering (buffering=1) makes sense only for text writing. The buffer sits in process memory, so a crash or os._exit() loses whatever was not flushed, and flush() itself only passes the data to the OS; getting it onto the actual disk takes os.fsync(). For console output the same effect is available as print(..., flush=True).
Examples
import io
buf = io.StringIO()
buf.write('text')
buf.getvalue() # → 'text' StringIO не нуждается в flush
f = open('test_io.txt', 'w', buffering=1)
f.write('line\n')
f.flush() # строки сразу сбрасываются
f.close()
open('test_io.txt').read() # → 'line\n'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy