ARTICLE AD BOX
I haven't found it explicitly stated in Python docs, but in C (Ref: fopen):
Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek(3) or fsetpos(3) operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect).
Therefore:
import os # File 'rplus.txt' initially contains: Hello, world! with open('rplus.txt', 'w') as file: file.write('Hello, world!') with open('rplus.txt', 'r+') as file: print(file.read(5)) # Outputs: Hello file.seek(0, os.SEEK_CUR) # Added file positioning between read/write file.write(', Python!') # Attempt to overwrite the rest file.seek(0) print(file.read())Output:
Hello Hello, Python!The reason is buffering. The file implementation uses buffered I/O to be more efficient, and .read(5) internally reads an entire buffer-worth of data from disk. If the file was larger, the write wouldn't be at the end of the file, but at wherever the file pointer ended up after the buffer read.
Explore related questions
See similar questions with these tags.

