Where did my write go?
Here is a spooky and extremely common surprise: you write() to a file, and the data is not in the file yet.
Why? Asking the OS to move bytes to disk is expensive (unit 6 taught you that giving up the CPU and waiting on hardware costs real time). So Python does not send every little write() to the OS. It collects your writes in a buffer, a chunk of memory inside your process, and hands the whole batch over later: when the buffer fills, when you call flush(), or when the file is closed.
Batching turns thousands of tiny expensive trips into a few big ones. The price is a window of time where your program thinks it wrote, but the file has not changed.
Code exercise · python
Run this. We write "hello" but keep the file open, then peek at the file from a second reader. The data does not appear until flush() pushes the buffer out.
When this bites in real life
- Crashes lose buffered data. If a program dies before flushing, the buffered writes never reach the file. This is why log lines sometimes vanish right before a crash, exactly the lines you needed.
- Another process reads too early. Program A writes a status file, program B reads it and sees nothing, like our demo.
with open(...)is the fix for most cases: closing a file flushes it, and thewithblock guarantees the close even on exceptions. That is the habit you have been using all course.
One level deeper: flush() hands bytes to the OS, which keeps its own cache before the physical disk. Databases that absolutely cannot lose data call an extra OS-level force (os.fsync) at the cost of real slowness. Layers of buffers, all the way down.
Code exercise · python
Your turn. This logger keeps the file open (real loggers do) but the read below sees nothing. Add ONE line so the event is visible to the reader while the file stays open. Expected read: 'event 1\n'
Quiz
A script crashes with an exception. Its last three print-to-file log lines are missing from the log. What most likely happened?