Skip to content Skip to sidebar Skip to footer

How Do I Exclude Directories When Using Os.walk()? Other Methods Haven't Worked

So far the following code has been nothing but stubborn: for root,subdirs,files in os.walk(topDir): for fileName in files: if fileName.endswith(('0.tif','0.png')):

Solution 1:

Trythis

exclude = set(["faces","animations"])
for root,subdirs,files in os.walk(topDir):
    subdirs[:] = [d for d in set(subdirs)-exclude]

Post a Comment for "How Do I Exclude Directories When Using Os.walk()? Other Methods Haven't Worked"