-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_tool.py
More file actions
57 lines (47 loc) · 2.11 KB
/
diff_tool.py
File metadata and controls
57 lines (47 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import difflib
def compare(file1_path, file2_path):
"""
Compare two files.
"""
with open(file1_path, 'r') as file1, open(file2_path, 'r') as file2:
diff = difflib.unified_diff(file1.readlines(), file2.readlines(), fromfile=file1_path, tofile=file2_path)
return list(diff)
def compare_folders(folder1_path, folder2_path):
"""
Compare two folders recursively.
"""
diff = difflib.Differ()
folder1_contents = sorted(os.listdir(folder1_path))
folder2_contents = sorted(os.listdir(folder2_path))
folder1_only = set(folder1_contents) - set(folder2_contents)
folder2_only = set(folder2_contents) - set(folder1_contents)
diff_result = list(diff.compare(folder1_contents, folder2_contents))
return folder1_only, folder2_only, diff_result
def main():
input_path1 = input("Enter the path of the first file or folder: ")
input_path2 = input("Enter the path of the second file or folder: ")
if os.path.isfile(input_path1) and os.path.isfile(input_path2):
# Comparing files
diff_result = compare(input_path1, input_path2)
if not diff_result:
print("No differences found between the files.")
else:
output_file_name = "diff_output.txt"
with open(output_file_name, 'w') as output_file:
output_file.write('\n'.join(diff_result))
print("Differences between the files are saved in", output_file_name)
elif os.path.isdir(input_path1) and os.path.isdir(input_path2):
# Comparing folders
folder1_only, folder2_only, diff_result = compare_folders(input_path1, input_path2)
if not diff_result:
print("No differences found between the folders.")
else:
output_file_name = "diff_output.txt"
with open(output_file_name, 'w') as output_file:
output_file.write('\n'.join(diff_result))
print("Differences between the folders are saved in", output_file_name)
else:
print("Invalid input. Please provide paths to either two files or two folders.")
if __name__ == "__main__":
main()