Search code examples
pythonpython-2.7mime-typescentos7

why is mimetypes.guess_type('a.json') not working in centos 7


In Centos, why is python 2.7 prebuilt library mimetypes.guess_type not returning mimetype for json files? https://docs.python.org/2/library/mimetypes.html#

I am using guess_type in mimetypes and it returns different value in centos/ubuntu. What's the pythonic way to deduce mimetype from filename in different OS?

In ubuntu 14.04, it returns the correct mime type

>>> import mimetypes
>>> mimetypes.guess_type('a.json')
('application/json', None)

But in Centos7

>>> import mimetypes
>>> mimetypes.guess_type('a.json')
(None, None)
>>> mimetypes.guess_type('a.JSON')
(None, None)

I checked the similar question and suggested answer, it will work only if the file of given content exists... How to find the mime type of a file in python?


Solution

  • On CentOS 7 you will need to install a package named "mailcap":

    yum search mailcap
    

    This is described as "Helper application and MIME type associations for file types".

    After installing mailcap, the following will work:

    >>> import mimetypes
    >>> mimetypes.guess_type('a.json')
    ('application/json', None)