使用文件管理的最大递归深度
我必须创建一个搜索引擎,该引擎将搜索包含文本文件的目录(文件夹)中的特定单词。
例如,假设我们正在搜索一个称为X的目录中的“机器”一词。我要实现的是扫描X及其子目录内的所有TXT文件。
调用Python对象时,我将获得最大的递归深度。
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几点:
list = os.listdir(path)
- 这仅给您 evely <>相对 file/pathnames,但是以下需要 absolute absolute < /em>一个(例如Open
),一旦您超出了cwd
?返回
语句放错了位置:它在 first txt-file之后返回?glob。 glob()
andpath.rglob()
:为什么不使用它们?path.absolute()
未记录,我不会使用它。您可以使用path.resolve.resolve.resolve.resolve()
?事件
什么都不做:我认为您在检索它后应该更新主词典吗?list
作为变量名称 - 您正在覆盖对内置list()
的访问。这是
path.rglob()
的建议:如果您想自己实现递归,那么您可以做类似的事情:
A couple of points:
list = os.listdir(path)
- this gives you only relative file/pathnames, but the following requires absolute ones (for example theopen
) once you're outside yourcwd
?return
statement is misplaced: it returns after the first txt-file?os.walk()
,glob.glob()
andPath.rglob()
: Why don't you use them?Path.absolute()
isn't documented, I wouldn't use it. You could usePath.resolve()
instead?occurences
in the recursion step: I think you should update the main dictionary after retrieving it?list
as a variable name - you're overriding access to the built-inlist()
.Here's a suggestion with
Path.rglob()
:If you want to implement the recursion for yourself, then you could do something like: