I have several files to iterate through, some of them several million lines long. One file can have more than 500 MB. I need to prep them by searching and replacing '| |'
string with '|'
string.
However, the following code runs into a "Memory error". How to rework the code to search and replace the files by line to save RAM? Any ideas? This is not about reading the large file line by line as rather replacing string line by line and avoiding issue with transforming list into string and vice versa.
import os
didi = self.lineEdit.text()
for filename in os.listdir(didi):
if filename.endswith(".txt"):
filepath = os.path.join(didi, filename)
with open(filepath, errors='ignore') as file:
s = file.read()
s = s.replace('| |', '|')
with open(filepath, "w") as file:
file.write(s)