1

I wouldlike to compare 2 files and display if it's ok or not (validation). Here a small picture to describe what I wouldlike to do :

Description

def open_file_txt(file_name):  

    with open(file_name, 'r') as f:
        x = f.readlines()

    return x

def validation(file_name1, file_name2):

    file1 = open_file_txt(file_name1)
    file2 = open_file_txt(file_name2)

    for elem in file1:
        for elem2 in file2:
            #### code 

I don't know if I started well but I imagine structure like that...

AC-1994
  • 83
  • 9
  • Try this, https://stackoverflow.com/questions/36873485/compare-md5-hashes-of-two-files-in-python – sushanth May 17 '20 at 03:48
  • In your example, you want to compare files row-wise. And each file's rows are sorted in alphabetical order, and are unique (no duplicates), hence e.g. `Test A, TestD, TestF` with newline separators instead of commas. So you can store them in a Python `set`, you don't even need `collections.Counter` (an item can't occur more than once). – smci May 17 '20 at 04:05

2 Answers2

3

readlines() returns a list so once you have both lists, you can get the elements that are common to both lists with set(lst1).intersection(lst2). Futhermore, since you want to check if all elements of lst1 are in lst2, you can do set(lst1).difference(lst2) and make sure your result is empty as that returns the elements of lst1 that where not found in lst2.

In this example lst1 and lst2 are the two lists in from your readlines() calls.

Hope that helps.

Touten
  • 190
  • 12
0

The proper method to use is set.issubset.

set(file1).issubset(file2)

Note that issubset accepts a list (at least in python 3.7) so you only need to convert one list.

angeldeluz777
  • 319
  • 3
  • 4