Home > Net >  How to compare two files and display contents which are not present in one in a third file
How to compare two files and display contents which are not present in one in a third file

Time:06-24

I am facing a problem in displaying contents which are not present in two files with respect to one.

What I am doing is: There are 10 json files in my folder, i am picking them according to there extension suppose 4 of them have _api.json at the end, so i will take them up, the main program which i wrote in python validates the json files, then i add the file name of the json files in names.txt. Now suppose the next time i run, someone has added 5 more files with _api.json and deleted 2 files which were initially present, with .json at the end. Now my program runs again and filenames are stored in names.txt, and at the beginning of the program, filenames of the previous run are added in a new file names2.txt now i want to have a txt file which contains the names of the 2 files which are not present in this run by comparing these two files.

Running the program once, file name get stored in names.txt

names

Running the program again changing the extension, names.txt get stores in names2.txt and names.txt gets updated

names_again

names2.txt contains the previous runs value.

names2

How to create a file missing.txt which compares the above two files and displays: test1.json,test2.json and test3.json

If there is any better way like using json or something better please tell.

Thank you

CodePudding user response:

Read files as lists of lines, convert both to set() and do set1 - set2

#lines1 = open('names.txt').read().split('\n')
#lines2 = open('names2.txt').read().split('\n')

lines1 = ['test0.json']
lines2 = ['test0.json', 'test1.json', 'test2.json', 'test3.json']

set1 = set(lines1)
set2 = set(lines2)
set3 = set([""])    # to remove empty lines

missing = set2 - set1 - set3
print('missing:', missing)

text = '\n'.join(missing)
print('text:')
print(text)

with open('missing.txt', 'w') as f:
    f.write(text)

Result:

missing: {'test1.json', 'test2.json', 'test3.json'}
text:
test1.json
test2.json
test3.json
  • Related