Jan-22-2024, 12:05 PM
Sorry to be back so soon.... Any ideas why my code causes Differ to crash? (NB The 3,7 is just an arbitrary number for testing a range). The "for x in R1" added just for a test works fine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import difflib import itertools difference = difflib.Differ() file1 = "gonk1.txt" file2 = "gonk2.txt" result_file = "resultfile.txt" cnt1 = 0 with open (result_file, "w" ) as f3, open (file1, "r" ) as f1, open (file2, "r" ) as f2: r1 = itertools.islice(f1, 3 , 7 ) r2 = itertools.islice(f2, 3 , 7 ) for x in r1: print (x) for x in difference.compare(r1, r2): print (x) |
Error: File "c:\Users\gonks\Desktop\python_work\compare_v4.py", line 18, in <module>
for x in difference.compare(r1, r2):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\difflib.py", line 859, in compare
cruncher = SequenceMatcher(self.linejunk, a, b)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\difflib.py", line 182, in __init__
self.set_seqs(a, b)
File "C:\Program Files\Python312\Lib\difflib.py", line 194, in set_seqs
self.set_seq2(b)
File "C:\Program Files\Python312\Lib\difflib.py", line 248, in set_seq2
self.__chain_b()
File "C:\Program Files\Python312\Lib\difflib.py", line 296, in __chain_b
n = len(b)
^^^^^^
TypeError: object of type 'itertools.islice' has no len()