0

I want to do this without loading the entire file into memory or creating an alternate text file. The text file is very large. So it doesn't make sense to load the entire file into memory. Creating a new file(even for temporary purposes) is also not preferred. Is there anything else I could do?

Faihan
  • 113
  • 1
  • 9

2 Answers2

0

Simple do

with open("sample.txt") as infile:
    for line in infile:
        if 'hello' in line:
            modify_func(line)

This actually won't load your entire file into memory but only a line at a time, which is then treated as garbage data and erase after an iteration, so it will efficiently modify your file.

kennysliding
  • 2,783
  • 1
  • 10
  • 31
  • The real problem is in the modify_func(). How do I define it? What is the logic behind that function? As far as I know, iterating though the file line by line only gets a copy of what's in the current line and it is not possible to edit that line in the file directly within this scope. – Faihan Dec 28 '20 at 15:19
  • If I can get the lines in the above mentioned way, append them to a string and write it to a new file(or the source file itself), then the string will contain the entire data (which is very huge) in the memory. – Faihan Dec 28 '20 at 15:21
  • My problem is not in the reading part sir, It is in the writing part. – Faihan Dec 28 '20 at 15:22
0

If you know a particular position in the file that you want to write to then you can seek it to that point and then read/ write at that point.

Removing a line requires you to open it properly and rewrite it to source excluding that line

example for seek

lineno=301
linelen=14
with open('foo.txt','w+') as file:
    file.seek(lineno * (linelen+2))
    line=file.readline()
    file.seek(1,-linelen-2)
    file.write('hello\n')