Pdiff: Pretty side-by-side diff

1.1.5 · active · verified Wed Apr 15

Pdiff is a command-line utility for generating pretty side-by-side differences between two text files. It is inspired by `ydiff` and `icdiff`, focusing on enhancing readability with color highlighting. The current version is 1.1.5, and it is actively maintained with infrequent but consistent updates.

Warnings

Install

Imports

Quickstart

Pdiff is invoked from the command line. This quickstart demonstrates how to use `subprocess.run` in Python to execute `pdiff` and capture its output, comparing two temporary text files. Remember to have `pdiff` installed and available in your system's PATH for this to work.

import subprocess
import os

# Create dummy files for demonstration
file1_content = """Line 1: Hello world
Line 2: This is a test file.
Line 3: Some text that is different.
Line 4: Common line.
"""

file2_content = """Line 1: Hello world!
Line 2: This is a modified test file.
Line 3: Completely new line here.
Line 4: Common line.
Line 5: An extra line in file2.
"""

with open('file1.txt', 'w') as f:
    f.write(file1_content)

with open('file2.txt', 'w') as f:
    f.write(file2_content)

print("Running pdiff on file1.txt and file2.txt:\n")

try:
    # Execute pdiff as a subprocess
    # Use text=True (or universal_newlines=True for older Python) to capture stdout as string
    # The '--' separates options from filenames, useful if filenames start with '-' or similar.
    result = subprocess.run(['pdiff', '--', 'file1.txt', 'file2.txt'], capture_output=True, text=True, check=True)
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print(f"Error running pdiff: {e.stderr}")
except FileNotFoundError:
    print("Error: 'pdiff' command not found. Please ensure pdiff is installed and in your PATH.")
finally:
    # Clean up dummy files
    if os.path.exists('file1.txt'):
        os.remove('file1.txt')
    if os.path.exists('file2.txt'):
        os.remove('file2.txt')

view raw JSON →