26 lines
559 B
Python
26 lines
559 B
Python
|
"""
|
||
|
Project: Regex Search
|
||
|
Page 230
|
||
|
"""
|
||
|
|
||
|
import re
|
||
|
import sys
|
||
|
from pathlib import Path
|
||
|
|
||
|
directory = Path(sys.argv[1])
|
||
|
keys = "|".join(sys.argv[2:])
|
||
|
regex = re.compile(f"({keys})")
|
||
|
result = []
|
||
|
|
||
|
for path in directory.glob("**/*.txt"):
|
||
|
text = open(path, "r").readlines()
|
||
|
show_header = True
|
||
|
for i, line in enumerate(text):
|
||
|
if re.match(regex, line):
|
||
|
if show_header:
|
||
|
result.append(f"\n{path}")
|
||
|
show_header = False
|
||
|
result.append(f"{i + 1}\t{line.strip()}")
|
||
|
|
||
|
print("\n".join(result).strip())
|