Checking Common File Types

Chris Angelico rosuav at gmail.com
Sun Dec 1 17:49:54 EST 2013


On Mon, Dec 2, 2013 at 5:27 AM, jade <jadec375 at msn.com> wrote:
> file_sigs = {'\xFF\xD8\xFF':('JPEG','jpg'),
>  '\x47\x49\x46':('GIF','gif')}
>
>     file_sig = fh.read(4)

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.

ChrisA



More information about the Python-list mailing list