Getting MIME Types on Files & Buffers in Python

Through programming languages I’ve been through, finding MIME types is not that kinda easy, the language might not have a built-in function for that or if they have it won’t be that efficient, I can recall in PHP I’d activated the finfo module to find MIME type of existing files.

Now in Python, we have a built-in module with is the mimetypes but isn’t that efficient because last time I used it to find a MIME type of a .html file; I couldn’t get an accurate result, below is a simple function I use to get MIME type of a file in python

But that couldn’t give me an efficient result, no result for the .html file. I went into search (Google & StackOverflow…LOL) and found the magic module, this module is not installed by default in Python so firstly we’ll get this module. If you are using PIP then just