The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a file comparison checker

A file comparison checker typically compares the contents of two files and reports whether they are identical or different. For a more advanced checker, it can highlight the differences line-by-line or even character-by-character.

Here’s a detailed Python script for a file comparison checker that:

  • Compares two text files.

  • Shows if files are identical or different.

  • If different, displays line-by-line differences with line numbers.

  • Supports large files efficiently.

python
def compare_files(file1_path, file2_path): differences = [] line_num = 0 with open(file1_path, 'r', encoding='utf-8') as f1, open(file2_path, 'r', encoding='utf-8') as f2: while True: line1 = f1.readline() line2 = f2.readline() line_num += 1 # Both files ended if not line1 and not line2: break # Check line differences if line1 != line2: differences.append({ "line": line_num, "file1": line1.rstrip('n'), "file2": line2.rstrip('n') }) if not differences: print("Files are identical.") else: print(f"Files differ in {len(differences)} line(s):n") for diff in differences: print(f"Line {diff['line']}:") print(f" File1: {diff['file1']}") print(f" File2: {diff['file2']}") print() # Example usage file1 = 'file1.txt' file2 = 'file2.txt' compare_files(file1, file2)

This script opens two files simultaneously, reads them line by line, and compares each line. When differences are found, it collects and prints them after the full comparison.

If you want a deeper character-by-character diff or output in a format like unified diff, you can use Python’s built-in difflib module:

python
import difflib def file_diff(file1_path, file2_path): with open(file1_path, 'r', encoding='utf-8') as f1, open(file2_path, 'r', encoding='utf-8') as f2: f1_lines = f1.readlines() f2_lines = f2.readlines() diff = difflib.unified_diff(f1_lines, f2_lines, fromfile='file1.txt', tofile='file2.txt', lineterm='') diff_output = 'n'.join(diff) if diff_output: print("Files differ:n") print(diff_output) else: print("Files are identical.") # Example usage file_diff(file1, file2)

This provides a standard diff output that clearly shows insertions, deletions, and changes. Let me know if you want me to tailor the checker for binary files or add a GUI.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About