File Compare with difflib.context_diff

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Mar 19 02:42:18 CET 2009


En Wed, 18 Mar 2009 21:02:42 -0200, Emile van Sebille <emile at fenx.com>  
escribió:

> JohnV wrote:
>   > What I want to do is compare the old data (lets day it is saved to a
>> file called 'lastdata.txt') with the new data (lets day it is saved to
>> a file called 'currentdata.txt') and save the new appended data to a
>> variable
>
> You may get away with something like: (untested)
>
> newdata=open('currentdata.txt').read()[len(open('lastdata.txt').read()):]

The same idea, but without reading unneeded bits:

oldsize = os.stat('lastdata.txt').st_size
with open('currentdata.txt','rb') as curf:
   f.seek(oldsize)
   newdata = f.read()

This assumes the 'currentdata.txt' file *never* shrinks nor is overwritten  
- not very realistic, so you'll need some additional checks.

-- 
Gabriel Genellina




More information about the Python-list mailing list