0

I have learned that there are 'r' mode for read, 'w' for write, and 'a' for appending. And we have also have the '+', 't' 'b' combination.

if I use 'a' or 'a+', I can only append new writings at the end. even if I use 'r+', I can only write in specific locations to cover the old content. Example: original 123456, if I f_obj.seek(2) and f_obj.write('abc'), it will become 12abc6

So is there a way I can write at seek(2) and end up with 12abc3456?

jxie0755
  • 1,682
  • 1
  • 16
  • 35
  • 1
    No operating system that I've ever heard of has the ability to insert data into the middle of a file. You'd need to rewrite the entire rest of the file, or better yet write everything to a new file (and then possibly rename it the same as the old file). – jasonharper Sep 25 '17 at 20:52
  • 1
    Files are sequences of bytes. You can't insert, only overwrite. This means you need to *move the other bytes you otherwise would overwrite*. – Martijn Pieters Sep 25 '17 at 20:58

0 Answers0