google colaboratory - Issue during file handling in python while 'write' operation - Stack Overflow

There is a test.txt file which has 'Hello, world' in it already. I am running the code shown

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 badge
Add a comment  | 

1 Answer 1

Reset to default 1

Explicitly 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信