Feb-14-2023, 09:03 PM
(This post was last modified: Feb-15-2023, 02:38 PM by deanhystad.)
I understand your confusion. r+ is confusing. When you write, were do you write?
The logical answer is that writing starts at the file pointer. When you open a file with r+, the file pointer starts at 0. When you read a file, the file pointer moves to the end. This explain why writing happens at the start of the file when you open a file with r+ and do a write. It also explains why you write at the end of a file when you open a file, do a read(), then do a write.
What isn't so well explained is what happens if you open a file, read one line, and do a write.
When I run this:
And look at what happens when I set the file pointer before doing a write.
Bizarre!
I cannot find anything documenting the expected behavior in Python, so I looked at C++ documetnation. I found this:
https://cplusplus.com/reference/cstdio/fopen/
This doesn't explicitly say where it will be repositioned, but implies it is repositioned to the end of the file.
I think that the takeaway is that you should never write anywhere except at the end of a file, and that r+ is an odd mode.
The logical answer is that writing starts at the file pointer. When you open a file with r+, the file pointer starts at 0. When you read a file, the file pointer moves to the end. This explain why writing happens at the start of the file when you open a file with r+ and do a write. It also explains why you write at the end of a file when you open a file, do a read(), then do a write.
What isn't so well explained is what happens if you open a file, read one line, and do a write.
When I run this:
with open("test.txt", "w") as file: file.write("One\nTwo\nThree\n") with open("test.txt", "r+") as file: file.readline() print(file.tell()) file.write("Four\n")The file pointer is 5:
Output:5
But it creates this file:Output:One
Two
Three
Four
This looks like the file pointer was 13,, not 5, for the write()And look at what happens when I set the file pointer before doing a write.
with open("test.txt", "w") as file: file.write("One\nTwo\nThree\n") with open("test.txt", "r+") as file: file.readline() file.seek(file.tell()) # Move file pointer to the reported file pointer location. file.write("Four\n")It creates this file:
Output:One
Four
hree
Here the write() starts at the file position.Bizarre!
I cannot find anything documenting the expected behavior in Python, so I looked at C++ documetnation. I found this:
https://cplusplus.com/reference/cstdio/fopen/
Quote:For files open for update (those which include a "+" sign), on which both input and output operations are allowed, the stream shall be flushed (fflush) or repositioned (fseek, fsetpos, rewind) before a reading operation that follows a writing operation. The stream shall be repositioned (fseek, fsetpos, rewind) before a writing operation that follows a reading operation (whenever that operation did not reach the end-of-file).
This doesn't explicitly say where it will be repositioned, but implies it is repositioned to the end of the file.
I think that the takeaway is that you should never write anywhere except at the end of a file, and that r+ is an odd mode.