पायथन स्टैंडर्ड लाइब्रेरी में स्ट्रिंग्स/फ़ाइलों के बीच अंतर खोजने के लिए विशेष रूप से difflib मॉड्यूल है। difflib.unified_diff फ़ंक्शन को कॉल करके डिफ़ प्राप्त करें।
उदाहरण
मान लें दो फ़ाइलें हैं:
file1: Hello People of the world file2: Hello People from India
डिफ़ लेने का कोड
import difflib
with open('file1') as f1:
f1_text = f1.read()
with open('file2') as f2:
f2_text = f2.read()
# डिफ़ खोजें और प्रिंट करें:
for line in difflib.unified_diff(f1_text, f2_text, fromfile='file1', tofile='file2', lineterm=''):
print(line)
आउटपुट
--- file1 +++ file2 @@ -1,5 +1,4 @@ Hello People -of -the -world +from +India