Untitled
unknown
plain_text
a year ago
1.1 kB
13
Indexable
import difflib
def compare_files(file1, file2, output_file):
# Read the contents of both files
with open(file1, 'r') as f1, open(file2, 'r') as f2:
file1_lines = f1.readlines()
file2_lines = f2.readlines()
# Create a Differ object and compare the two files
differ = difflib.Differ()
diff = list(differ.compare(file1_lines, file2_lines))
# Store changes in file2 not present in file1
changes = []
for line in diff:
if line.startswith('+ '): # Lines added in file2
changes.append(line[2:]) # Remove the '+ ' prefix
# Print changes to console
print("Changes in file2 not present in file1:")
for change in changes:
print(change, end='')
# Write changes to output file
with open(output_file, 'w') as out_file:
out_file.write("Changes in file2 not present in file1:\n")
out_file.writelines(changes)
# Replace 'file1.py', 'file2.py', and 'output.txt' with actual file paths
compare_files('file1.py', 'file2.py', 'output.txt')
Editor is loading...
Leave a Comment