我有一个小脚本遍历工作区并将 .asc 类型的相同文件名添加到字典中的列表中。但是,我想省略工作区中某些文件夹中的所有 .asc 文件(以蓝色突出显示)。如何忽略以蓝色突出显示的文件夹中的 .asc 文件,或者仅包含位于子块文件夹中的 .asc 文件?
import os, collections
from collections import defaultdict
workspace = r'C:\my\workspace'
# Get a list of all files in subfolders
rasters = defaultdict(list)
for root, dirs, files in os.walk(workspace):
for file in files:
if file.endswith(".asc"):
rasters[file].append(os.path.join(root, file))
rasters = rasters.values()