Search code examples
pythonlistfilecomparedifference

How to compare two files, but ignore if on different lines (when comparing two files)


I have this code

 with open(newconfig, 'r') as file1:  # New File
    with open(goldconfig, 'r') as file2:  # Standard File
        difference = set(file1).difference(file2)

difference.discard('\n')

diff_file = input("INFO: Select what to name the difference(s) : ")
with open(diff_file, 'w') as file_out:
    for line in difference:
        file_out.write("** WARNING: Difference found in New Config:\n " + line + "\n")
        print("WARNING: Difference in file: " + line)

print("\n\n")
print("INFO: Difference File Created: " + diff_file)

but I want to ignore if the file has the same word, but on different lines so for example

List one: TOM123 TOM1234 TOM12345

List Two: TOMA TOMB TOM123 TOM1234 TOM12345

Difference:

TOMA TOMB


Solution

  • you can try this:

    def open_file_and_return_list(file_path):
        list = []
        with open(file_path, 'r') as f:
            line = f.readline()
            while line:
                list.append(line)
                line = f.readline()
        return list
    
    def clean_new_line(list):
        for i in range(len(list)):
            if "\n" in list[i]:
                list[i] = list[i].replace("\n", "")
        return list
    
    
    if __name__ == "__main__":
        list1 = open_file_and_return_list(r"path\File1.txt")
        list2 = open_file_and_return_list(r"path\File2.txt")
        list1 = clean_new_line(list1)
        list2 = clean_new_line(list2)
        diff = []
        for obj in list1:
            if obj not in list2:
                diff.append(obj)
        for obj in list2:
            if obj not in list1:
                diff.append(obj)
    
        print(diff)