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?
Asked
Active
Viewed 85 times
0
-
1So how do you decide which line to update? – kennysliding Dec 28 '20 at 14:59
-
Not exactly in the middle, but any line somewhere in the middle. – Faihan Dec 28 '20 at 15:02
-
So then how do you decide which line to update? A line that starts with `the`? A line that has 20 words? You have to be specific on that – kennysliding Dec 28 '20 at 15:04
-
I want to remove every line that has 'hello' in it (for example) – Faihan Dec 28 '20 at 15:06
2 Answers
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
-
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')

Aditya Gupta
- 96
- 8