Following code counts number of image in each sub directory. how to delete a sub directory if images in sub-directory are more than 2.
n13 is main directory=> which have 300 sub-directories(1...300)=> each sub-directory have images.
output:
Images:2, Directory:1
Images:3, Directory:2
Images:4, Directory:3
import os
path='C:/n13/'
def count_em(path):
x = 0
for root, dirs, files in os.walk(path):
files_count = (len(files))
x = x + 1
print("Images:",files_count,"Directory:",x)
return files_count
You can use shutil.rmtree() to delete a folder with its sub-directories and files.