Skip to content

Instantly share code, notes, and snippets.

@gothicx
Last active February 22, 2021 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gothicx/b63ee1af2228d5168e62f7a863ee9233 to your computer and use it in GitHub Desktop.
Save gothicx/b63ee1af2228d5168e62f7a863ee9233 to your computer and use it in GitHub Desktop.
Find valid images using Python code
#!/usr/bin/env python
#
# Find and clean up invalid images
#
# bY Marco Rodrigues 2021 - <gothicx@gmail.com>
#
import imghdr
from os import walk, path, remove
dir_moments = '/volume1/homes/user/Drive/Moments'
extensions = ['.jpg', 'jpeg', '.png', '.gif']
exclude_dirs = ['@eaDir']
for root, dirs, files in walk(dir_moments):
dirs[:] = [d for d in dirs if d not in exclude_dirs]
for fn in files:
for ext in extensions:
if fn.lower().endswith(ext):
filename = root + "/" + fn
if imghdr.what(filename) == None:
print "Removing file" + filename + "..."
remove(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment