There is a test.txt
file which has 'Hello, world' in it already. I am running the code shown below
with open('test.txt','r+') as f:
print(f.read(3))
f.write("wow!")
f.flush()
Final content of test.txt
: 'Hello, worldwow!'
My understanding says it should have been 'Helwow!world'
.
Even if I mention f.flush()
or not the final content of file remains same.
I tried implementing this to get more insight:
with open("test.txt", "r+") as f:
print(f.tell())
print(f.read(3))
print(f.tell())
f.flush()
f.write("wow!")
print(f.tell())
and the output was:
0
Hel
3
16
I could not make out as to why the cursor after write goes to 16. My current Python version is 3.10.0, I tried the same in Google Colab but it's the same.
There is a test.txt
file which has 'Hello, world' in it already. I am running the code shown below
with open('test.txt','r+') as f:
print(f.read(3))
f.write("wow!")
f.flush()
Final content of test.txt
: 'Hello, worldwow!'
My understanding says it should have been 'Helwow!world'
.
Even if I mention f.flush()
or not the final content of file remains same.
I tried implementing this to get more insight:
with open("test.txt", "r+") as f:
print(f.tell())
print(f.read(3))
print(f.tell())
f.flush()
f.write("wow!")
print(f.tell())
and the output was:
0
Hel
3
16
I could not make out as to why the cursor after write goes to 16. My current Python version is 3.10.0, I tried the same in Google Colab but it's the same.
Share Improve this question edited Mar 21 at 12:45 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 21 at 12:43 BinaryBinary 11 bronze badge1 Answer
Reset to default 1Explicitly seeking to write position
You make assumptions about how the file is read and where the write seek position is. Mode "r+"
supports write-after-seek, however, you have to explicitly seek to that position.
with open('test.txt','r+') as f:
f.seek(f.tell())
f.write("wow!")
f.flush()
test.txt now contains:
Helwow!orld!
See also Difference between modes a, a+, w, w+, and r+ in built-in open function?
Effect of buffered reading for text files
By default text I/O is always buffered in python. Any file that is smaller than io.DEFAULT_BUFFER_SIZE
is read in one chunk. If the text file is larger, the write position will be exactly at the end of the buffer.
with open('test.txt', 'w') as f:
f.write('a' * (io.DEFAULT_BUFFER_SIZE + 100))
with open('test.txt', 'r+') as f:
s = f.read(3)
f.write("wow!")
will insert the wow!
at io.DEFAULT_BUFFER_SIZE=8192
Using binary file I/O, however, works as expected:
with open('test.bin','r+b') as f:
s = f.read(3)
f.write(b"wow!")
will insert wow!
at position 3.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744353163a4570095.html
评论列表(0条)