Python:获取包含特定文件扩展名的文件夹

Python:获取包含特定文件扩展名的文件夹,python,Python,我试图找到如何获得一个文件夹列表,其中包含一个特定的文件扩展名,这将给我只文件夹,只包含唯一的文件夹。而且是最快的方式 那有一条班轮吗 所以如果我有 c:\folderA\test1.tga c:\folderA\test2.tga c:\folderA\test3.tga c:\folderB\test4.tga 我想要 c:\folderA\ c:\folderB\ 干杯不是一句台词,而是: def findFolder(root, suffix): for dirName in os.

我试图找到如何获得一个文件夹列表,其中包含一个特定的文件扩展名,这将给我只文件夹,只包含唯一的文件夹。而且是最快的方式

那有一条班轮吗

所以如果我有 c:\folderA\test1.tga c:\folderA\test2.tga c:\folderA\test3.tga c:\folderB\test4.tga

我想要 c:\folderA\ c:\folderB\

干杯

不是一句台词,而是:

def findFolder(root, suffix):
  for dirName in os.listdir(root):
    dirPath = os.path.join(root, dirName)
    if os.path.isdir(dirPath):
      for fileName in os.listdir(dirPath):
        if fileName.endswith(suffix):
          yield dirPath  # or dirName, whatever you like better
          break

有一个内衬,但是如果你用更多的内衬,看起来会更好

set(folder for folder, subfolders, files in os.walk('/') for file_ in files if os.path.splitext(file_)[1] == '.png')

还有一个能帮我解决作业的吗?谢谢,这正是我想要的!