What really is the difference between file.write() and file.flush()?
From Python's official documentation, I get this:
flush() Force bytes held in the buffer into the raw stream. A BlockingIOError should be raised if the raw stream blocks.
write(b) Write the bytes-like object, b, and return the number of bytes written. When in non-blocking mode, a BlockingIOError is raised if the buffer needs to be written out but the raw stream blocks.
To the best of my newbie understanding, they both transfer information currently held in memory to a file on disk. The difference however that write() also returns information on how much information was stored, while flush() performs this task immediately. Is that correct? In what circumstances would it then be preferable to apply write() over flush() and vice-versa?
flushtakes no arguments... they're not the same at all. The doc you copied shows an argument forwriteand none forflush.flushforces data written usingwriteto be dumped to the media.write()sends the data from your program into the IO subsystem where it may be buffered.flush()tells the IO subsystem that you would like to commit any data that has been buffered.write,flushwill do nothing.