1

I have a very large text file. In that file, using python, I want to change line 3333 from AABB to AA_BB. Since I know exactly which line I want to change, it seems pointless to open the entire file with file.readlines() as suggested here, here, and here.

I there an efficient way to do this? Something like sed -i -e '3333s/AABB/AA_BB/'?

Sky
  • 379
  • 3
  • 13
  • 1
    You don't need to read the entire file, but you will have to seek from the beginning of the file to the line you wish to edit. This is because you have to count the number of new lines. – byxor Jan 22 '20 at 11:26
  • Refer this link https://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python – Ragu Natarajan Jan 22 '20 at 11:41
  • 1
    There is no other way to do this. Firstly you don't know where in the file the change needs to be apart from the line number. Secondly the change adds at least one character to the file. Both of these points add up to the need to read the whole file *and* to write back to the whole file. – quamrana Jan 22 '20 at 12:13

0 Answers0