Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string insertion in big file

Tags:

python

file

I have big file (few GBs) with text.

For example, it have next text:

Hello, World!

I need to insert word " funny" at 5 position, and offset the rest of text:

Hello, funny World!

How I can don't read all file for offsetting rest? Or how I can optimise this operation?

Thanks.

like image 398
Alexander Ruliov Avatar asked Jun 19 '11 20:06

Alexander Ruliov


2 Answers

You can't. Plain text files cannot be shrunk or expanded in the beginning or middle of the file, but only at the end.

like image 62
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 12:09

Ignacio Vazquez-Abrams


Well you cant, please see this for more info How do I modify a text file in Python?

like image 41
Adithya Surampudi Avatar answered Sep 19 '22 12:09

Adithya Surampudi