1

Yesterday I have been implementing a small Python scripts that checks difference between two files (using difflib), printing the result if there is any, exiting with code 0 otherwise.

The precise method, difflib.unified_diff() is returning a generator on the diffs found. How can I test this generator to see if it needs to be printed? I tried using len(), sum() to see what was the size of this generator but then it is impossible to print it.

Sorry to ask such a silly question but I really don't see what is the good practice on that topic.

So far this is what I am doing

import difflib
import sys

fromlines = open("A.csv").readlines()
tolines = open("B.csv").readlines()
diff = difflib.unified_diff(fromlines, tolines, n=0)
if (len(list(diff))):
    print("Differences found!")
    # Recomputing the generator again: how stupid is that!
    diff = difflib.unified_diff(fromlines, tolines, n=0)
    sys.stdout.writelines(diff)
else:
    print("OK!")
Julien B.
  • 88
  • 6

3 Answers3

1

You're already converting your generator to a list, so you don't need to rebuild it.

diff = list(difflib.unified_diff(fromlines, tolines, n=0))
if diff:
    ...
    sys.stdout.writelines(diff)
else:
    ...

You don't even need to convert the generator to a list if you don't want by using a simple flag:

diff = difflib.unified_diff(fromlines, tolines, n=0)
f = False
for line in diff:
    if not f:
        print("Differences found!")
        f = True
    sys.stdout.write(line)

if not f:
    print("OK!")
sloth
  • 99,095
  • 21
  • 171
  • 219
0

You could convert the generator into a list.

diff = list(difflib.unified_diff(fromlines, tolines, n=0))
b3orn
  • 322
  • 1
  • 5
0

I think you can't, and the proper way is probably to generate all data until you raise StopIteration and then get the length of what you have generated.

What's wrong with :

import difflib
import sys

fromlines = open("A.csv").readlines()
tolines = open("B.csv").readlines()
diff = difflib.unified_diff(fromlines, tolines, n=0)
difflines = list(diff)
if len(difflines) :
    sys.stdout.writelines(difflines)
else:
    print("OK!")
shodanex
  • 14,975
  • 11
  • 57
  • 91