'Compare two files for text match
I have a text representation of a file tree (treesample.csv), and another file with a listing of files(filesToTest.txt). I'm trying to read treesample.csv, and search it for each line in filesToTest.txt, and print the matching line. From what I can tell, my nested for loops below should do the trick, but I can't get it to match.
with open("c:/temp/treesample.csv") as file:
treelines = file.readlines()
with open("c:/temp/filesToTest.txt") as file2:
testlines = file2.readlines()
for line in treelines:
for line2 in testlines:
if line2 in line:
print("found !", line)
Solution 1:[1]
import filecmp
f1 = "file1.txt"
f2 = "file2.txt"
# shallow comparison
result = filecmp.cmp(f1, f2)
print(result)
# deep comparison
result = filecmp.cmp(f1, f2, shallow=False)
print(result)
or with see the diff
# reading files
f1 = open("1.txt", "r")
f2 = open("2.txt", "r")
i = 0
for line1 in f1:
i += 1
for line2 in f2:
# matching line1 from both files
if line1 == line2:
# print IDENTICAL if similar
print("Line ", i, ": IDENTICAL")
else:
print("Line ", i, ":")
# else print that line from both files
print("\tFile 1:", line1, end='')
print("\tFile 2:", line2, end='')
break
# closing files
f1.close()
f2.close()
Solution 2:[2]
you can use something like this:
import difflib
with open('file1.txt') as file_1:
file_1_text = file_1.readlines()
with open('file2.txt') as file_2:
file_2_text = file_2.readlines()
# Find and print the diff:
for line in difflib.unified_diff(
file_1_text, file_2_text, fromfile='file1.txt',
tofile='file2.txt', lineterm=''):
print(line)
or if you want another way to do that. refer to the link below https://www.geeksforgeeks.org/compare-two-files-line-by-line-in-python/
Solution 3:[3]
If just want to find the matching lines in two files then can iterate over one of list of lines and check if it is found in the second list. Don't need nested for loops.
f1 = "1.txt"
f2 = "2.txt"
with open(f1, "r") as file:
treelines = file.readlines()
with open(f2, "r") as file2:
testlines = file2.readlines()
for line in treelines:
if line in testlines:
print("found !", line.rstrip())
Can do the same thing with list comprehension.
matches = [line for line in treelines if line in testlines]
for line in matches:
print("found !", line.rstrip())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tomerar |
| Solution 2 | Vismay Tiwari |
| Solution 3 |
