File comparison script not working Python -


i wrote custom file comparison script based on nature of files i'm checking. file read dictionary , script tries hash lines determine if line exists in other file.

f = open('test.txt', 'r') f2 = open('test2.txt', 'r')  m1 = {} m2 = {} line in f:     m1[line] = line line2 in f2:     m2[line2] = line2  k in m1:     try:         l = m2[k]      except keyerror:         print m1[k]  f.close() f2.close() 

i put trash line in 1 of files , script did not print out. why didn't detect trash line?

following @peterwood's suggestion,

def get_line_set(fname):     open(fname) inf:         return set(line.rstrip() line in inf)  f1 = get_line_set("test.txt") f2 = get_line_set("test2.txt")  only_in_f1 = f1 - f2 if only_in_f1:     print("\nthe following lines appear in f1 not in f2:")     print("\n".join(sorted(only_in_f1)))  only_in_f2 = f2 - f1 if only_in_f2:     print("\nthe following lines appear in f2 not in f1:")     print("\n".join(sorted(only_in_f2))) 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -