Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to compare two text files and result in the first string in the comparison file that does not match. Kindly provide a sample way to use this module?

When I try something like:

result = difflib.SequenceMatcher(None, testFile, comparisonFile)

I get an error saying:

the object of type 'file' has no len.

1 Answer

0 votes
by (107k points)

In order to remove the error, you just need to pass strings to difflib.SequenceMatcher, not files:

# Like so

difflib.SequenceMatcher(None, str1, str2)

# Or just read the files in

difflib.SequenceMatcher(None, file1.read(), file2.read())

If you are a beginner and want to know more about Python, then  do check out the Python certification course.

Related questions

0 votes
1 answer
asked Nov 24, 2020 in Python by ashely (50.2k points)
+2 votes
3 answers
0 votes
1 answer
asked Dec 23, 2020 in Linux by blackindya (18.4k points)
0 votes
1 answer
asked Jul 30, 2019 in Python by Eresh Kumar (45.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...