Skip to content
File Methods

flush()

Flush write buffers directly to disk without closing the file.

file.flush()

file.flush() clears the internal I/O write buffer and forces all buffered text/bytes to be written immediately to the storage device.

Returns

None.

Try it

Run it and change it
import tempfile
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as f:
    f.write('streaming log line\n')
    f.flush()
    f.seek(0)
    print(f.read().strip())

Worth knowing

  • Real-time logs & data safety: Essential when writing long-running service logs or critical transaction data so data is not lost during crashes.

Related entries