import os
from PIL import Image
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
path = os.path.join(root, name)
try:
img = Image.open(path) # open the image file
img.verify() # verify that it is, in fact an image
except (IOError, SyntaxError) as e:
print('Bad file:', path) # print out the names of corrupt files
os.remove(path)
for root, dirs, files in os.walk(".", topdown=False):
for name in dirs:
path = os.path.join(root, name)
if not os.listdir(path):
os.rmdir(path)