Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views2 pages

Compare Py

compare contents of 2 folders assuming the file hierarchy is the same or almost the same (Python)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Compare Py

compare contents of 2 folders assuming the file hierarchy is the same or almost the same (Python)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import os

import difflib
import sys

def get_all_files(folder):
file_list = []
for root, _, files in os.walk(folder):
for file in files:
relative_path = os.path.relpath(os.path.join(root, file), folder)
file_list.append(relative_path)
return set(file_list)

def read_file_lines(filepath, ignore_comments):


try:
with open(filepath, 'r', encoding='utf-8') as f:
return [line.rstrip() for line in f.readlines() if not (ignore_comments
and (line.lstrip().startswith('*') or line.lstrip().startswith('//')))]
except Exception as e:
return [f"[Error reading file: {e}]"]

def format_diff(file, content1, content2):


diff = list(difflib.ndiff(content1, content2))
differences = []
line_num1, line_num2 = 1, 1
current_block = []

for i, line in enumerate(diff):


if line.startswith(' '):
line_num1 += 1
line_num2 += 1
if current_block:
differences.append('\n'.join(current_block) + '\n')
current_block = []
elif line.startswith('-'): # Line only in file1
if not current_block:
start_line = line_num1
current_block.append(f"file1> {line[2:]}")
line_num1 += 1
elif line.startswith('+'): # Line only in file2
current_block.append(f"file2> {line[2:]}")
line_num2 += 1

if i == len(diff) - 1 or (line.startswith(' ') and current_block):


if current_block:
differences.append(f"Line number: {start_line} - {line_num1 - 1}\n"
+ '\n'.join(current_block) + '\n')
current_block = []

return f"Differences in {file}:\n\n" + '\n'.join(differences) + "\n\n"

def compare_folders(folder1, folder2, ignore_comments, output_file='output.txt'):


files1 = get_all_files(folder1)
files2 = get_all_files(folder2)
all_files = files1.union(files2)
differences = []

for file in all_files:


path1 = os.path.join(folder1, file)
path2 = os.path.join(folder2, file)
if file not in files1:
differences.append(f"File only in {folder2}: {file}\n\n")
elif file not in files2:
differences.append(f"File only in {folder1}: {file}\n\n")
else:
content1 = read_file_lines(path1, ignore_comments)
content2 = read_file_lines(path2, ignore_comments)

if content1 != content2:
diff_text = format_diff(file, content1, content2)
differences.append(diff_text)

with open(output_file, 'w', encoding='utf-8') as f:


if differences:
f.writelines(differences)
else:
f.write("No differences found.\n")

print(f"Comparison complete. Results saved in {output_file}")

if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python compare.py <folder1> <folder2> [ignoreComments]")
sys.exit(1)

folder1 = sys.argv[1]
folder2 = sys.argv[2]
ignore_comments = len(sys.argv) > 3 and sys.argv[3] == "ignoreComments"

compare_folders(folder1, folder2, ignore_comments)

You might also like