title: Python Script to detect broken images
body: <p>I wrote a python script to detect broken images and count them, 
  The problem in my script is it detects all the images and does not detect broken images. How to fix this. I refered :</p>
  
  <p><a href="https://stackoverflow.com/questions/889333/how-to-check-if-a-file-is-a-valid-image-file">How to check if a file is a valid image file?</a> for my code</p>
  
  <p>My code</p>
  
  <pre><code>import os
  from os import listdir
  from PIL import Image
  count=0
  for filename in os.listdir('/Users/ajinkyabobade/Desktop/2'):
      if filename.endswith('.JPG'):
       try:
        img=Image.open('/Users/ajinkyabobade/Desktop/2'+filename)
        img.verify()
       except(IOError,SyntaxError)as e:
           print('Bad file  :  '+filename)
           count=count+1
           print(count)
  </code></pre>