I have to create a search engine, that will search for a specific word inside a directory (folder) that contains text files.
For example, assume that we are searching for the word "machine" in a certain directory called X. What I want to achieve is to scan all the txt files inside X and its subdirectories as well.
I am getting maximum recursion depth exceeded while calling a Python object.
import os
from pathlib import Path
def getPath (folder) :
fpath = Path(folder).absolute()
return fpath
def isSubdirectory (folder) :
if folder.endswith(".txt") == False :
return True
else :
return False
def searchEngine (folder, word) :
path = getPath(folder)
occurences = {}
list = os.listdir (path) #get a list of the folders/files in this path
#assuming we only have .txt files and subdirectories in our folder :
for k in list :
if isSubdirectory(k) == False :
#break case
with open (k) as file :
lines = file.readlines()
for a in lines :
if a == word :
if str(file) not in occurences :
occurences[str(file)] = 1
else :
occurences[str(file)] += 1
return occurences
else :
return searchEngine (k, word)