Aug-10-2020, 06:45 AM
My problem is as follows:
suppose I have 2 huge (like 10GB) text files as follows:
file1:
My (non-working) code below:
suppose I have 2 huge (like 10GB) text files as follows:
file1:
ad 1a 2b 3c ...file2:
10 0 2b 45 ...What I need to do is to iterate the 2 files simultaneously line by line (i.e. line 1 in file1 and line 1 in file2 together) and conditionally replace that line in file2 (e.g. if lines are equal, replace line2 with '0'). Note that this has to be done in place because the files are huge and cannot be loaded in memory. Can someone suggest a way?
My (non-working) code below:
with open("file1", 'r') as fileA,open("file2", 'r+') as fileB: for line1, line2 in zip(fileA, fileB): if line1 == line2: #replace line2 -> 0