Python Forum

Full Version: Difference Between 2 files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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