compare - Comparing two files using difflib in python -


i trying compare 2 files using difflib. after comparing want print "no changes" if no difference detected. if difference in lines. want print line.

i tried this:

with open("compare.txt") f, open("test.txt") g:             flines = f.readlines()             glines = g.readlines()         d = difflib.differ()         diff = d.compare(flines, glines)         print("\n".join(diff)) 

it print contents of file if "no changes" detected. want print "no changes" if there no difference.

check see if first character in each element has + or - @ start (marking line having changed):

with open("compare.txt") f, open("test.txt") g:         flines = f.readlines()         glines = g.readlines()         d = difflib.differ()         diffs = [x x in d.compare(flines, glines) if x[0] in ('+', '-')]         if diffs:             # rows changes         else:             print('no changes') 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -