First of all, the "l_few" (file with fewer lines) is file_1 according to the text, so I swapped the file names to change as little as possible in your original program.
Then I came up with this, hope it helps, note that I didn't change much, just enough to make it print what you need:
Further simplification:
Then I came up with this, hope it helps, note that I didn't change much, just enough to make it print what you need:
with open ('file_2','r') as l_few : f2=l_few.readlines() sp23 = [s.split(',')[3].strip() for s in f2] # collect all the terms you need to compare with open ('file_1','r') as f1: for lf1 in f1: lf1=lf1.strip() sp1 = lf1.split(",") if sp1[3] in sp23: pass # here we have a match so print nothing # spL2=lf2.split(",") #print (" File 2 Line matched --> "+lf2) #break else : print (lf1) # print the non-matching line #breakOutput:
Output:03/28/2021,P,16,LINE1
03/28/2021,P,9,LINE3
03/28/2021,P,8,LINE5
03/28/2021,S,95,LINE6
03/28/2021,S,1,LINE7
03/28/2021,P,46,LINE8
So the error you made was to compare first with one term then with the other so, for each line you had at least one mismatch.Further simplification:
with open ('file_2','r') as l_few : f2=l_few.readlines() sp23 = [s.split(',')[3].strip() for s in f2] # collect all the terms you need to compare with open ('file_1','r') as f1: for lf1 in f1: lf1=lf1.strip() sp1 = lf1.split(",") if sp1[3] not in sp23: print (lf1) # print the non-matching line