top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Checking Common File Types in Python?

+2 votes

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. I'm familiar with basic Python but this is the first time I've attempted anything like this and I'm looking for a little help or a point in the right direction?

file_sigs = {'xFFxD8xFF':('JPEG','jpg'),  'x47x49x46':('GIF','gif')}
def readFile(): filename = r'c:/temp/downloads'  fh = open(filename, 'r')  file_sig = print '[*] check_sig() File:',filename #, 'Hash Sig:', binascii.hexlify(file_sig) 
posted Dec 1, 2013 by Sumit Pokharna

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+2 votes

file_sigs = {'xFFxD8xFF':('JPEG','jpg'), 'x47x49x46':('GIF','gif')}

file_sig =

You're reading in four bytes, but your signatures are three bytes long. :)

After that, all you need to do is look up file_sig in the file_sigs dictionary, and see if anything's there. Look at the Python docs for the dict type; there's an easy way to do this.

answer Dec 1, 2013 by Satish Mishra
Similar Questions
+2 votes

I saw three different types of quotes in python. For me all gave same result.
Can anyone explain what could be the reason of having three different quotes in python and which one is used in what circumstances ?

0 votes

The type() builtin according to python docs, returns a "type object".

And in this module is bunch of what I assume are "type objects". Is this correct?

And type(), aside from being used in as an alternative to a class statement to create a new type, really just returns the object class, doesn't it?

>>> import types
>>> a = type(1)
>>> b = (1).__class__
>>> c = int
>>> d = types.IntType
>>> a is b is c is d

If type() didn't exist would it be much more of a matter than the following?:

def type(x): 
 return x.__class__

What is the purpose of type()?
What exactly is a "type object"? Is it a "class"?
What is the purpose of the types module?

I understand the purpose of isinstance and why it's recommended over something like (type(1) is int). Because isinstance will also return True if the object is an instance of a subclass.

>>> class xint(int):
 def __init__(self):

>>> x = xint()
>>> type(x) is int
>>> isinstance(x, int)
+1 vote

A list can contain different types of elements but I am not able to understand how max () method work with different types of data elements.

0 votes

I am having trouble matching Python data types with those of MySQL. MySQL has about 7 basic data types including Blobs, Binaries, etc. It also has a rich selection of geometry types. In addition, types like INT have 7 or 8 different options like Primary Key, zero fill, auto inc, etc. I can't seem to find anything in python to match these. I am trying to build a for an existing database that was created with MySQL Workbench.

I do not wish to use anything other than MySQL because of the complexity of my storage needs (searchable text, PDF, Audio, Video and Photos). The text searches will often be word phrase searches through thousands of documents. I need the speed and the data type flexibility.

How do I build or modify a to do this. I really don't want to write a manually. That would really be a major pain. Workbench to the database an import to Django and edit is my choice. Unfortunately, the produced has lost most of the functionality of the original database and I can't seem to figure out how to fix it.

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.