Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
r+ mode in files
#3
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:
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.
Reply


Messages In This Thread
r+ mode in files - by grkiran2011 - Feb-14-2023, 02:03 PM
RE: r+ mode in files - by rob101 - Feb-14-2023, 03:24 PM
RE: r+ mode in files - by grkiran2011 - Feb-15-2023, 11:54 PM
RE: r+ mode in files - by deanhystad - Feb-14-2023, 09:03 PM
RE: r+ mode in files - by grkiran2011 - Feb-15-2023, 11:56 PM
RE: r+ mode in files - by DeaD_EyE - Feb-16-2023, 02:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  things that work in terminal mode but not in sublime mode alok 4 2,952 Aug-11-2021, 07:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020