Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore different newline char with python filecmp

I have small script that compares files

import filecmp
filecmp.cmp(path1, path2)  

This code returns True if files are similar But it returns False in case when newline are different. path1 have Linux newline and path2 have Windows newline. I want get True in case if files differs only with newline character. Is it possible without editing files?

like image 835
nick_gabpe Avatar asked Oct 23 '25 02:10

nick_gabpe


1 Answers

This is not possible to do with the filecmp module as it can only use stat to compare files and won't let you override the comparison.

You could do something with itertools as noted here:

from itertools import izip

def areFilesIdentical(filename1, filename2):
    with open(filename1, "rtU") as a:
        with open(filename2, "rtU") as b:
            # Note that "all" and "izip" are lazy
            # (will stop at the first line that's not identical)
            return all(myprint() and lineA == lineB
                       for lineA, lineB in izip(a, b))
like image 97
meyer9 Avatar answered Oct 24 '25 15:10

meyer9