| from difflib import ndiff |
|
|
| def merge_file1_file2(file1_path, file2_path, output_path): |
| with open(file1_path, 'r') as f1, open(file2_path, 'r') as f2: |
| file1_lines = f1.readlines() |
| file2_lines = f2.readlines() |
|
|
| diff = list(ndiff(file1_lines, file2_lines)) |
| merged = [] |
|
|
| for line in diff: |
| if line.startswith(" "): |
| merged.append(line[2:]) |
| elif line.startswith("- "): |
| merged.append(line[2:]) |
| elif line.startswith("+ "): |
| merged.append(line[2:]) |
| |
|
|
| with open(output_path, 'w') as out: |
| out.writelines(merged) |
|
|
| |
| merge_file1_file2("file1.txt", "file2.txt", "merged.txt") |