difflib and intelligent file differences

Marco Mariani marco at sferacarta.com
Thu Mar 26 12:37:57 EDT 2009


Marco Mariani wrote:

>> If the lines are really sorted, all you really need is a merge,

For the archives, and for huge files where /usr/bin/diff or difflib are 
not appropriate, here it is.


> #!/usr/bin/env python
> 
> import sys
> 
> def run(filea, fileb):
>     p = 3
>     while True:
>         if p&1: a = filea.readline()
>         if p&2: b = fileb.readline()
>         if not a or not b:
>             break
>         elif a == b:
>             p = 3
>         elif a < b:
>             sys.stdout.write('-%s' % a)
>             p = 1
>         elif b < a:
>             sys.stdout.write('+%s' % b)
>             p = 2
> 
>     for line in filea.readlines():
>         sys.stdout.write('-%s' % line)
> 
>     for line in fileb.readlines():
>         sys.stdout.write('+%s' % line)
> 
> 
> if __name__ == '__main__':
>     run(file(sys.argv[1]), file(sys.argv[2]))




More information about the Python-list mailing list