我需要以递归方式查找名称包含子字符串" Bar"的文件夹下的所有路径。在Python 2.7中
那是文件夹结构
Foo | ------ Doug | | | --------CandyBar | ---------MilkBar
我需要获取列表["Foo/Doug/CandyBar", "Foo/MilkBar"]
现在我可以使用os.walk和glob.glob编写一堆循环来获取此列表,但我想知道我是否错过了一种更简单的技术。
答案 0 :(得分:3)
使用发电机可能是个不错的选择
import os
res = (path for path,_,_ in os.walk("path") if "bar" in path)
注意:我使用“/”作为根路径,因为我的系统类似于unix。如果你在Windows上用“C:\”(或任何你想要的)替换“/”
优点:
示例:
# returns immediately
res = (path for path,_,_ in os.walk("/") if "bar" in path)
#I have to wait (who knows how much time)
res = [path for path,_,_ in os.walk("/") if "bar" in path]
示例:
res = (path for path,_,_ in os.walk("/") if "bar" in path)
# the for starts at no time
for path in res:
# at each loop I only wait the time needed to compute the next path
print(path) # see the path printed as it is computed
res = [path for path,_,_ in os.walk("/") if "bar" in path]
# the for starts only after all paths are computed
for path in res:
# no wait for each loop.
print(path) # all paths printed at once
示例:
res = (path for path,_,_ in os.walk("/") if "bar" in path)
path_store = []
for path in res:
# I'm only interested in paths having odd length
# at the end of the loop I will use only needed memory
if(len(path)%2==1):
path_store.append(path)
示例:
res = (path for path,_,_ in os.walk("/") if "bar" in path)
path_store = []
count = 10
for path in res:
# I'm only interested in paths having odd length
if(len(path)%2==1):
count -= 1
path_store.append(path)
# I'm only interested in the first 10 paths.
# Using generator I waited only for the computation of those 10 paths.
# Using list you will always wait for the computation for all paths
if( count <= 0 ):
break
CONS:
您不能将索引与生成器一起使用。你只能得到下一个项目。
如果你想要一个包含所有路径的列表,你必须在列表中进行转换(所以最好使用列表解析)
生成器是一次性前进(获取下一个元素后无法返回)
如果你想保留一些“路径”,你必须将它存储在某个地方(如列表),否则它将丢失
在每次迭代时都会丢失。 在循环结束时, res 已用尽,无法再使用。 我必须在 path_store 列表中存储我感兴趣的路径。
path_store = []
for path in res:
# I'm only interested in paths having odd length
if(len(path)%2==1):
path_store.append(path)
path = next(res) # Error StopIteration
答案 1 :(得分:2)
试试这个:
import os
[x for x, _, _ in os.walk("path") if "bar" in x and os.path.isdir(x)]