Home > OS >  Displaying the difference between two files
Displaying the difference between two files

Time:01-29

I am learning Python, and have a query. Sorry for inconvenience.
I have 2 text files as follow :
File1.txt:

1
2
3

File2.txt:

1
2
3
4 

I want to print what's unique in File2.txt, the output should be 4. It would be great if it would be a python script.
Thanks in advance

CodePudding user response:

you could use the SVN-Tortoise where this tool is part of the main installation for simple purposes, because your will be able to see it right away visually or if it you like to do it with python then difflib, some toy example from Doug Hellmann

import difflib from difflib_data import *

d = difflib.Differ() diff = d.compare(text1_lines, text2_lines) print('\n'.join(diff))

CodePudding user response:

Giving you the exact code would not help you in learning, so let me give you hint which you can explore:

Read about readlines() in python -> It opens a text file and reads all the lines and saves them back in a list.
Read about finding difference between two lists ->stackoverflow solution

Once you are able to figure out both of these, you should be able to achieve the result you are aiming for.
Thank You.

  •  Tags:  
  • Related