Attempting to write into a file after reading it appends new data unexpectedly [duplicate]

17 hours ago 1
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.

answered Apr 5, 2025 at 15:20

Mark Tolonen's user avatar

2 Comments

Worth a bug report on the Python docs, imho.

2025-04-05T16:58:05.67Z+00:00

@AdonBilivit Only if you want to truncate. you may want to replace only a portion. But that wasn’t the question.

2025-04-06T12:29:00.013Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article