python - 使用 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 搜索?
解决方案
推荐阅读
- c# - 即使在使用 UnitTests c# 进行测试时也制作 NLog 日志
- java - 为什么 libgdx 按钮每次都不起作用?
- plugins - 是否有功能或插件显示包含文件与 Visual Studio Code 的顶级 .asp 文件内联?
- .net - 我可以使用 extern 来半引用其他 .net 程序集吗?
- c - 解析命令并将文件路径传递给 fopen
- javascript - 如何防止侧边栏中的图标移动?
- c# - ASP.NET SqlParameter
- javascript - Javascript中字符串匹配+替换的特例
- html - z-index 和转换问题
- c++ - 如果可推导类型有替换,可变参数模板类型推导会使编译器崩溃