04
Aug 2009
The mimetypes module provides a great interface for guessing the MIME types of files. Which is great, except mimetypes.guess_type() requires you pass it the filename to inspect. This makes sense on first thought, but what if you already have the data stored somewhere (say, as a blob)?
After much poking, it seems you can get around this limitation by using the undocumented magic module. Unfortunately, its API is a mess, so here is what you need to do:
>>> import magic
>>> _cookie = magic.open(magic.MAGIC_MIME)
>>> _cookie.load()
0
>>> a_string_or_buffer = 'this is plain text'
>>> _cookie.buffer(a_string_or_buffer)
'text/plain charset=us-ascii'
You can then extract the MIME type by simply split()ing the returned string and extracting its first element.
Thanks to Dan C for this tip.