Compare two text files in python




















Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. As part of some Python tests using the unittest framework, I need to compare two relatively short text files, where the one is a test output file and the other is a reference file.

It works fine if the test passes, but in the even of failure, there is not much help in the output:. Is there a better way of comparing two files as part of the unittest framework, so some useful output is generated in case of mismatch?

To get a report of which line has a difference, and a printout of that line, use assertListEqual on the contents, e. All you need to do is add your own message for the error condition. You could use that each time you need to compare files, or put it in a function. You could also put the files in the forms of text string instead of arrays.

Isn't it better to compare the content of the two files. For example if they are text files compare the text of the two files, this will output some more meaningful error message. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to compare two files as part of unittest, while getting useful output in case of mismatch?

Ask Question. Asked 4 years, 10 months ago. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. Previous Compare two Files line by line in Python. Next Python: filecmp. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.

Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Python. Improve this question. Add a comment. Active Oldest Votes. Improve this answer. Rekha Rekha 79 1 1 silver badge 1 1 bronze badge. Comparing two text files in python? Hovo Hovo 1.

Regardless of whether this code works, it doesn't really explain how to do what the question asks. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog.



0コメント

  • 1000 / 1000