arcpy.da.Walk가 os.walk처럼 작동한다고 가정하면 (즉, dirnames에서 디렉토리를 제거하면 해당 디렉토리로 이동하는 것을 멈 춥니 다) 다른 for 루프를 추가하여 dirnames를 반복하고 필터를 적용해야합니다. 내가 이터레이터를 엉망으로 만들지 않고 remove를 호출 할 수 있도록 dirnames를 복사했음을 주목하라.
for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"):
# remove subdirectories that match pattern so they will not be walked
for dirname in dirnames[:]:
if 'dir1' in dirname:
dirnames.remove(dirname)
ArcGIS Resources에서 Cribbing, 당신은 여러 가지 방법으로 파일 이름을 필터링하는 정규식을 사용할 수 있습니다. 여기에 'ABC'를 가진 파일을 제거하는 정규 표현식의 예는 와일드 카드 슬롯에 'DEF'또는 'GHI'이다 :
import arcpy
import os
import re
workspace = "c:/data"
feature_classes = []
# i dont like abc, def or ghi features so I have a regex to match them
filter_classes_re = re.compile('fc_(abc|def|ghi)_feature$')
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
datatype="FeatureClass",
type="Polygon"):
for filename in filenames:
# only add to feature list if it doesn't match the bad guys
if filter_classes_re.match(filename) is None:
feature_classes.append(os.path.join(dirpath, filename))
# alternately, i could extract the wildcard part and compare it outside
# of the regex ... but this will be slower
filter_classes_re = re.compile('fc_(.*?)_feature$')
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
datatype="FeatureClass",
type="Polygon"):
for filename in filenames:
# extract the wildcard part
match = filter_classes_re.match(filename)
if match:
matched = match.group(1)
else:
matched = ''
if matched not in ('abc', 'def', 'ghi'):
feature_classes.append(os.path.join(dirpath, filename))
당신은 아무것도를 필터링하지 않습니다 보여 코드를 ... 당신이없는 'dir1'이라는 디렉토리. dirnames를 반복하고 'dir1'이 그 중 하나에 있는지 확인하는 inner for 루프가 필요합니다. – tdelaney
예 스크립트 스 니펫은 전체 디렉토리 및 피쳐 클래스를 삭제합니다. 그게 내 문제가 어디 전체 디렉토리가 디렉토리 내에서 몇 가지 기능을 클래스 싶지 않아요. – cbrannin
'directory1'이라는 디렉토리와 'dir1'을 포함하지만 'dir1'이라는 디렉토리가없는 'fc_dir1_feature'와 같은 디렉토리가 있습니다. 그래서 if 문이 정말로''dirnames :'에있는''directory1 ''이라면, 당신은 그 문제를 갖게 될 것입니다. 나는 이것을 해결하는 방법을 알고 대답을 쓸 것이라고 생각합니다. – tdelaney