Python 3.x 如何在操作系统中使用生成器查找类似包装器的函数?
我在python中有一个类似于Python 3.x 如何在操作系统中使用生成器查找类似包装器的函数?,python-3.x,recursion,generator,Python 3.x,Recursion,Generator,我在python中有一个类似于findcommand的函数。因此,基本上它将进入深度,直到到达m\u depth(maxdepth),并且如果在忽略目录中指定它,它将不会进入目录。它将返回在漫游中找到的文件列表。代码非常简单,并且使用递归 但是对于大量文件或更大深度的文件,递归需要时间,返回时列表会越来越大。所以我在寻找生成器是否可以使用,这样每次迭代的内存消耗至少会更少 我尝试了yielding结果,但是只要找到ignore\u dirs,它就会退出 这是我的代码: def find(sour
find
command的函数。因此,基本上它将进入深度,直到到达m\u depth
(maxdepth),并且如果在忽略目录中指定它,它将不会进入目录。它将返回在漫游中找到的文件列表。代码非常简单,并且使用递归
但是对于大量文件或更大深度的文件,递归需要时间,返回时列表会越来越大。所以我在寻找生成器是否可以使用,这样每次迭代的内存消耗至少会更少
我尝试了yield
ing结果,但是只要找到ignore\u dirs
,它就会退出
这是我的代码:
def find(source_d, m_depth, ignore_dirs):
'''
This method does a recursive listing of files/directories from a given
path upto maximun recursion value provide as m_depth.
:param source_d: Given source path to start the recursion from
:param m_depth: Maximum recursion depth [determines how deep the method will traverse through the file system]
:param ignore_dirs: this paths will not be traversed. List of strings.
'''
def helper_find(path, ignore_dirs, m_depth, curr_depth=1):
files = []
if any(ignore_sub_dir == os.path.split(path)[-1] for ignore_sub_dir in ignore_dirs):
return []
if m_depth < curr_depth:
return []
else:
things = os.listdir(path)
for thing in things:
if(os.path.isdir(os.path.join(path, thing))):
files.extend(helper_find(os.path.join(path, thing), ignore_dirs, m_depth, curr_depth+1))
else:
files.append(os.path.join(path, thing))
return files
return helper_find(source_d, ignore_dirs, m_depth)
def find(源数据、m深度、忽略目录):
'''
此方法从给定的目录中递归列出文件/目录
最大递归值的路径以m_深度提供。
:param source_d:从中开始递归的给定源路径
:param m m_depth:最大递归深度[确定方法将在文件系统中遍历的深度]
:param ignore_dirs:将不遍历此路径。字符串列表。
'''
def helper_find(路径,忽略目录,m_深度,当前深度=1):
文件=[]
如果有(ignore\u sub\u dir==os.path.split(path)[-1]用于ignore\u dirs中的ignore\u sub\u dir):
返回[]
如果m_深度<当前深度:
返回[]
其他:
things=os.listdir(路径)
对于事物中的事物:
if(os.path.isdir(os.path.join(path,thing)):
扩展(helper\u find(os.path.join(path,thing),ignore\u dirs,m\u depth,curr\u depth+1))
其他:
files.append(os.path.join(path,thing))
返回文件
返回helper\u find(source\u d、ignore\u dirs、m\u depth)
答案是肯定的,您可以使用(仅在Python 3中可用)创建递归生成器:
any(路径中的ignore\u sub\u dir…
的问题是假设我不想进入videos
dir。因此我将ignore\u dirs
作为[“videos”,“some”]
。现在我不想进入仅在源路径下出现的视频
。如果源路径本身包含视频
,那会怎么样。然后在第一步它会返回。希望你明白我的意思……尽管我已经针对各种场景对代码进行了单元测试,并且没有发现任何错误。返回的是一些额外的空列表g正在丢弃我的测试用例。是否可以删除返回的额外空列表?@AnirbanNag'tintinmj'如果在第一个条件下返回空列表,请尝试上面的更新版本(如果已删除此)
def find(source_d, m_depth, ignore_dirs):
'''
This method does a recursive listing of files/directories from a given
path upto maximun recursion value provide as m_depth.
:param source_d: Given source path to start the recursion from
:param m_depth: Maximum recursion depth [determines how deep the method will traverse through the file system]
:param ignore_dirs: this paths will not be traversed. List of strings.
'''
def helper_find(path, ignore_dirs, m_depth, curr_depth=1):
if not any(ignore_sub_dir == os.path.split(path)[-1] for ignore_sub_dir in ignore_dirs)and m_depth >= curr_depth:
things = os.listdir(path)
for thing in things:
if(os.path.isdir(os.path.join(path, thing))):
yield from helper_find(os.path.join(path, thing), ignore_dirs, m_depth, curr_depth+1)
else:
yield os.path.join(path, thing)
return helper_find(source_d, ignore_dirs, m_depth)