如何获取与子字符串匹配的文件夹名称?

时间:2018-01-23 00:27:43

标签: python python-2.7

我需要以递归方式查找名称包含子字符串" Bar"的文件夹下的所有路径。在Python 2.7中

那是文件夹结构

Foo
|
------ Doug
|        |
|        --------CandyBar
|
---------MilkBar

我需要获取列表["Foo/Doug/CandyBar", "Foo/MilkBar"]

现在我可以使用os.walk和glob.glob编写一堆循环来获取此列表,但我想知道我是否错过了一种更简单的技术。

2 个答案:

答案 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)]