Find the mime type of a given file using Magic – Python – Ubuntu

Install Magic

sudo apt-get install python-magic

Do the following in Python

import magic
def returnMimeType(locationOfFile): 
 m = magic.open(magic.MAGIC_MIME_TYPE) 
 m.load() 
 mimeType = m.file(locationOfFile) 
 return mimeType

Returns “application/pdf” for a PDF

Pretty simple really ;-)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s