Python Forum
Difference Between 2 files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Difference Between 2 files (/thread-23299.html)



Difference Between 2 files - enigma619 - Dec-20-2019

Hi

I've a question to compare 2 files.

My first file 'file_new' contain:
Output:
1 2 3 5 6 7
My second file 'file_old' contains:
Output:
1 2 3 4 5 6
I want to compare these two files.
My file_new contain "reality" now.

So my script is
#!/usr/bin/env python3
import difflib

file_old = "/tmp/file_old"
file_new = "/tmp/file_new"

diff = difflib.ndiff(open(file_old).readlines(),open(file_new).readlines())
print (''.join(diff))
Result is:
Output:
1 2 3 - 4 5 6 + 7
Is there any mean to retrieve simply - and + values?
- To put them on a new file?
- To put them in an array / temporary json?

I'm doing some tests and reading docs but for the moment not concluant.

Thanks for ideas and help

Alex


RE: Difference Between 2 files - buran - Dec-20-2019

maybe difflib:
https://docs.python.org/3.8/library/difflib.html
https://pymotw.com/3/difflib/index.html


RE: Difference Between 2 files - perfringo - Dec-21-2019

One should make unambiguous what does 'difference' means.

For example, using difflib.Differ() one will get differences as follows (provided data in files named diff_1.txt and diff_2.txt respectively):

with open('diff_1.txt', 'r') as x, open('diff_2.txt', 'r') as y:
    diff = difflib.Differ().compare(x.readlines(), y.readlines())
    different_rows = (value.strip() for value in diff if value.startswith(('+', '-')))
    print(*different_rows, sep='\n')


Output:
+ 4 + 6 - 6 - 7



RE: Difference Between 2 files - Gribouillis - Dec-21-2019

Simply use filter()
import difflib
import io

f_new = io.StringIO('''\
1
2
3
5
6
7
''')

f_old = io.StringIO('''\
1
2
3
4
5
6
''')

def my_ndiff(*args):
    return filter(lambda x: not x.startswith(' '), difflib.ndiff(*args))

if __name__ == '__main__':
    diff = my_ndiff(f_old.readlines(), f_new.readlines())
    print(''.join(diff))
Output:
- 4 + 7