首页 > 解决方案 > 使用 Python rglob 时如何从搜索中排除目录?

问题描述

这是代码:

# Now create libraries for any asys Simulation Models that are present in the module
asysdir = moduleRootDir / asys_sys_tail
asys_systems = []
if asysdir.is_dir():
    asys_systems = [x for x in asysdir.iterdir() if
                (x.is_dir() and (x.name != '.asys_edit'))]

asyslibslist = []
for sys in asys_systems:
    libname = "lib_asys_" + sys.name
    lib = vu.add_library(libname)
    for ext in extensions:
        matches = set(item for item in sys.rglob(str("*") + str(ext)))
        for match in matches:
            # Check for duplications in the "rtlfiles" list
            if match.parent.name == 'mentor':
                logger.debug("Match to /mentor/ file!! " + str(match.absolute()))
                continue
            elif match.name in [item.name for item in DupList]:
                logger.debug("Item in DupList! : " + str(match.name))
                continue
            elif match.name in GlobalExclusions:
                continue
            else:
                logger.debug("Adding to lib " + str(libname) + " :" + str(match.absolute()))
                lib.add_source_file(match.absolute())
                DupList.append(match)
    asyslibslist.append(lib)
    # Print the DupList after each module
    print_duplist(DupList)

此代码在名为“asys_systems”的文件夹中开始搜索。它检查其中存在的文件夹并将其添加到集合 asys_systems 中。然后,它在 asys_systems 集合的每个文件夹中搜索文件。

我没有使用 Python 的经验,但需要更改此代码以限制递归搜索。例如,我们有这样的文件夹结构:

asys_system_1
  sub_folder_1
  sub_folder_2

我希望搜索 asys_system_1 中的所有文件,以及 sub_folder_2 但不是 sub_folder_1。

如何排除某些子文件夹被 sys.rglob 搜索?

标签: pythonfile-search

解决方案


推荐阅读