Last active
February 8, 2019 22:44
-
-
Save jmbjr/4b0c1f35c9168f61cd57ae81211b2df4 to your computer and use it in GitHub Desktop.
simple routine to scan a directory and delete files that aren't valid image types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imghdr | |
import os | |
#this script will scan every file in a directory and delete any that are not a valid image type that imghdr recognizes. | |
#see https://docs.python.org/3.1/library/imghdr.html | |
# The following image types are recognized, as listed below with the return value from what(): | |
# | |
# Value Image format | |
# 'rgb' SGI ImgLib Files | |
# 'gif' GIF 87a and 89a Files | |
# 'pbm' Portable Bitmap Files | |
# 'pgm' Portable Graymap Files | |
# 'ppm' Portable Pixmap Files | |
# 'tiff' TIFF Files | |
# 'rast' Sun Raster Files | |
# 'xbm' X Bitmap Files | |
# 'jpeg' JPEG data in JFIF or Exif formats | |
# 'bmp' BMP files | |
# 'png' Portable Network Graphics | |
def checkimgages(directory): | |
list_of_files = os.listdir(directory) | |
for ff in list_of_files: | |
thepath = os.path.join(directory, ff) | |
filetype = imghdr.what(thepath) | |
print('\n{} is filetype {}'.format(thepath, filetype)) | |
if filetype is None: | |
print('{} is not a valid filetype. Deleting file'.format(thepath)) | |
os.remove(thepath) | |
testdir = 'testdir' | |
checkimgages(testdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment