- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find difference between 2 files in Python?
The Python standard library has a module specifically for the purpose of finding diffs between strings/files. To get a diff using the difflib library, you can simply call the united_diff function on it.
For example, Lets say you have 2 files, file1 and file2 with the following content:
file1: Hello People of the world file2: Hello People from India
Now to take their diff use the following code:
import difflib with open('file1') as f1: f1_text = f1.read() with open('file2') as f2: f2_text = f2.read() # Find and print the diff: for line in difflib.unified_diff(f1_text, f2_text, fromfile='file1', tofile='file2', lineterm=''): print line
This will give the output:
--- file1 +++ file2 @@ -1,5 +1,4 @@ Hello People -of -the -world +from +India
- How to find minimum between 2 numbers using C#?
- How to find maximum between 2 numbers using C#?
- Python program to find difference between two timestamps
- Difference between Hadoop 1 and Hadoop 2
- Difference Between COCOMO 1 and COCOMO 2
- Find Maximum difference between tuple pairs in Python
- How to use Glob() function to find files recursively in Python?
- What is the difference between .py and .pyc files ?
- How to compare files in Python
- Program to find difference between node and a descendent in Python
- How to convert PDF files to Excel files using Python?
- How to find time difference using Python?
- Difference between files written in binary and text mode in C++
- What is difference between selenium 2 and selenium 3?
- What is difference between selenium 1 and Selenium 2