In short: I / O buffering. Two levels, even.
First, Python itself has I / O buffers. Thus, when you write all of these lines to a file, Python does not necessarily invoke syscall write right away - it does this when it flushes its buffers, which can be at any time when you invoke an entry before closing the file. It obviously will not affect you if you write at a level like you do syscalls.
But by contrast, the operating system will also use buffers. They work the same way - you do syscall to write to disk, the OS puts the data in its write buffer and will use it when other processes read this file. But it does not necessarily write it to disk - it can wait, theoretically, until you unmount this file system (possibly when it shuts down). This (part), why it might be a bad idea to disconnect a USB storage device without unmounting or “safely deleting”, for example, what you wrote on it, is not yet physically located on the device. Everything that the OS does does not depend on what language you write, or what part of the wrapper around your system calls.
In addition, both Python and the OS can do read buffering - essentially, when you read one line from a file, Python / OS expects you to be interested in the next few lines, and therefore reads them into main memory to avoid having to defer all the way to the disk later.
source share