Spaces:
Sleeping
Sleeping
File size: 1,057 Bytes
103c053 |
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 |
import os
class TextFinder:
def __init__(self, folder):
self.folder = folder
def find_matches(self, text):
matches = []
files = os.listdir(self.folder)
for file in files:
file_path = os.path.join(self.folder, file)
if os.path.isfile(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
index = content.find(text)
while index != -1:
start = max(content.rfind('\n', 0, index), content.rfind('.', 0, index))
end = min(content.find('\n', index), content.find('.', index))
if start != -1 and end != -1:
matches.append(content[start+1:end].strip())
index = content.find(text, index + 1)
return matches
# Example usage:
if __name__ == "__main__":
finder = TextFinder('example_folder')
matches = finder.find_matches('text_to_find')
print(matches)
|