2 Replies - 281 Views - Last Post: 02 December 2013 - 07:07 AM Rate Topic: -----

#1 MillyH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 20-November 13

Checking Files in Python

Posted 02 December 2013 - 03:02 AM

Hi,

I'm trying to create a script that checks all the files in my 'downloaded' directory against common file types and then tells me how many of the files in that directory aren't either a GIF or a JPG file.

import sys, os, binascii

def readfile():


dictionary = {'474946':(/>'GIF', 'gif'), 'ffd8ff':(/>'JPEG', 'jpeg')}
    try:
        files = os.listdir('C:\\Temp\\downloads')        
        for item in files:
            file = open('C:\\Temp\\downloads\\'+ item, 'r')
            file_sig = file.read(3)
            file_sig_hex = binascii.hexlify(file_sig)
                        
            if file_sig_hex in dictionary:
                print item + ' is a image file, it is a ' + file_sig

            else:
                print item + ' is not an image file, it is' +file_sig

            print file_sig_hex

    

    except:
        print 'Error. Try again'

    finally:
        if 'file' in locals():
            file.close()

def main():
 
    readfile()

if __name__ == '__main__':
    main()


As of right now my script prints out 'Error Try again' but when i comment out this part of the code;

if file_sig_hex in dictionary:
print item + ' is a image file' + file_sig

else:
print item + ' is not an image file, it is' +file_sig



it prints the file signatures to the screen, however what I'm trying to do with the if statement is tell me if the file is an image and give me is signature and if it is not, I want it to tell me and still give me it's signature and tell me what type of file it is. I know if I remove the try/except but I want to try and get it to work with it.

Can anyone help?

Is This A Good Question/Topic? 0
  • +

Replies To: Checking Files in Python

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,229
  • Joined: 12-December 12

Re: Checking Files in Python

Posted 02 December 2013 - 05:59 AM

Disable the try..except statement block temporarily, so that you will see a more helpful error message. Please post this error message.

It would also be helpful if you told us what print file_sig_hex outputs.
Was This Post Helpful? 0
  • +
  • -

#3 MillyH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 20-November 13

Re: Checking Files in Python

Posted 02 December 2013 - 07:07 AM

print file_sig_hex


outputs:
ffd8ff
ffd8ff
ffd8ff
504b03
3c2144
474946
ffd8ff
ffd8ff
ffd8ff
ffd8ff
ffd8ff
ffd8ff



with try/except removed but I don't receive any errors when I run the program which tells me that most of my code works.

if file_sig_hex in dictionary:
print item + ' is a image file' + file_sig

else:
print item + ' is not an image file, it is' +file_sig


and with this code without the try/except I get

poodle.jpg is a image file, it is a ffd8ff


But with try/except and this code;

if file_sig_hex in dictionary:
print item + ' is a image file' + file_sig

else:
print item + ' is not a common image file, it is' +file_sig


I get nothing printed apart from 'Error try again' which comes from here;

except:
        print 'Error. Try again'


but it is also picking up .bmp files not telling me that they're not common image files.

This post has been edited by MillyH: 02 December 2013 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1