2015-10-14 10:29:03 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from collections import defaultdict
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
2015-10-14 10:34:30 +08:00
|
|
|
parser.add_argument('inPlaceDir', type=str,
|
|
|
|
help="Directory to prune in-place.")
|
2015-10-29 19:50:15 +08:00
|
|
|
parser.add_argument('--numImagesThreshold', type=int,
|
2015-10-14 10:29:03 +08:00
|
|
|
help="Delete directories with less than this many images.",
|
|
|
|
default=10)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
exts = ["jpg", "png"]
|
|
|
|
|
|
|
|
for subdir, dirs, files in os.walk(args.inPlaceDir):
|
2015-10-14 10:34:30 +08:00
|
|
|
if subdir == args.inPlaceDir:
|
|
|
|
continue
|
2015-10-14 10:29:03 +08:00
|
|
|
nImgs = 0
|
|
|
|
for fName in files:
|
|
|
|
(imageClass, imageName) = (os.path.basename(subdir), fName)
|
|
|
|
if any(imageName.lower().endswith("." + ext) for ext in exts):
|
|
|
|
nImgs += 1
|
|
|
|
if nImgs < args.numImagesThreshold:
|
|
|
|
print("Removing {}".format(subdir))
|
|
|
|
shutil.rmtree(subdir)
|