Guest User

Untitled

a guest
Oct 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. # /usr/bin/env python2
  2. import difflib
  3. x = open('/path/to/file1', 'r').read()
  4. y = open('/path/to/file2', 'r').read()
  5. print 'n'.join(difflib.Differ().compare(x, y))
  6.  
  7. #!/usr/bin/env python2
  8. import commands
  9. import numpy as np
  10. def run_cmp(filename1, filename2):
  11. cmd = 'cmp --verbose %s %s'%(filename1, filename2)
  12. status, output = commands.getstatusoutput(cmd) # python3 deprecated `commands` module FYI
  13. status = status if status < 255 else status%255
  14. if status > 1:
  15. raise RuntimeError('cmp returned with error (exitcode=%s, '
  16. 'cmd="%s", output=n"%sn")'%(status, cmd, output))
  17. elif status == 1:
  18. is_different = True
  19. elif status == 0:
  20. is_different = False
  21. else:
  22. raise RuntimeError('invalid exitcode detected')
  23. return is_different, output
  24. if __name__ == '__main__':
  25. # create two binary files with different values
  26. # file 1
  27. tmp1 = np.arange(10, dtype=np.uint8)
  28. tmp1.tofile('tmp1')
  29. # file 2
  30. tmp2 = np.arange(10, dtype=np.uint8)
  31. tmp2[5] = 0xFF
  32. tmp2.tofile('tmp2')
  33. # compare using the shell command 'cmp'
  34. is_different, output = run_cmp(filename1='tmp1', filename2='tmp2')
  35. print 'is_different=%s, output=n"n%sn"'%(is_different, output)
Add Comment
Please, Sign In to add comment