Skip to content

Instantly share code, notes, and snippets.

@guoxiaolu
Created September 14, 2017 10:13
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 guoxiaolu/069a4fd14505d4b4ed1839e250614585 to your computer and use it in GitHub Desktop.
Save guoxiaolu/069a4fd14505d4b4ed1839e250614585 to your computer and use it in GitHub Desktop.
isjpg
# !/usr/bin/python
# coding=utf-8
from PIL import Image
import os
import sys
def is_jpg(filename):
try:
i=Image.open(filename)
return i.format =='JPEG'
except IOError:
return False
def remove_nojpg(fpath):
for root, dirs, files in os.walk(fpath):
for name in files:
fname = os.path.join(root, name)
if not os.path.isdir(fname):
_, ext = os.path.splitext(fname)
if ext == '.jpg':
if not is_jpg(fname):
os.remove(fname)
print 'remove %s\n'%(fname)
for name in dirs:
fname = os.path.join(root, name)
if not os.path.isdir(fname):
_, ext = os.path.splitext(fname)
if ext == '.jpg':
if not is_jpg(fname):
os.remove(fname)
print 'remove %s\n' % (fname)
if __name__ == "__main__":
try:
print 'prepare remove non-jpg files in %s\n'%(sys.argv[1])
remove_nojpg(sys.argv[1])
print 'end'
except Exception, e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment