I want to search for a pattern in text files and send the file where the match is found.
Using re.finditer because I have two matching lists (patterns and names of the files to email)
I want to send one file even if there are multiple matches.
Is this possible?
file1:
thre pat1 patterns
pat1
pat1
file2:
Only one pat2
bla bla
import re
import itertools
cc_files = ["file1", "file2"]
patlist = ["pat1", "pat2"]
prglist = ["prg1", "prg2"]
for (a, b) in itertools.zip_longest(patlist, prglist):
for cc_file in cc_files:
for i, line in enumerate(open(cc_file)):
for match in re.finditer(a, line):
print('Found on line %s: %s in file %s' % (i+1, match.group(), cc_file))
# email: filename=(b + "_" + cc_file)) </code>`
output:
Found on line 1: pat1 in file file1.txt
Found on line 2: pat1 in file file1.txt
Found on line 3: pat1 in file file1.txt
Found on line 1: pat2 in file file2.txt
I want:
pat1 in file1.txt
pat2 in file2.txt