You are seeing the effects of buffering. Disk I/O uses buffers to improve performance, and you have not written enough data to the buffer for it to flush.
Write more data, or close the file, both cause the buffer to be flushed. Alternatively, set the buffer size to a very small number (the number of bytes the buffer will hold):
with open('test.txt', 'w', 2) as ffile:
The options 0
and 1
have special meaning; 0
would disable buffering altogether (only available for binary mode files) and 1
is the default for text files (using line buffering, write a newline to flush).
That also means that if you have a text file, you could write a newline to trigger flush:
ffile.write('\n')
Last but not least, you could flush explicitly by using the file.flush()
method:
ffile.flush()