Spaces:
Runtime error
Runtime error
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) | |