2

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?

Community
  • 1
  • 1
satheeshram
  • 131
  • 1
  • 8
  • I'm mostly just guessing, but looking at the Python mimetypes code, it looks for files `/etc/mime.types`, `/etc/httpd/conf/mime.types`, etc, and reads those if they exist. Probably your Ubuntu install has one that maps `.json` while your Centos install doesn't. – torek Oct 26 '15 at 21:32
  • @torek. thanks. installing /etc/mime.types through rpm package(mailcap) solved it. – satheeshram Oct 26 '15 at 21:54

1 Answers1

5

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)
Simon Feltman
  • 1,069
  • 7
  • 8
  • 1
    Thanks. There seems to be multiple packages that can give /etc/mime.types in different distro [RPM search](http://rpmfind.net/linux/rpm2html/search.php?query=%2Fetc%2Fmime.types) – satheeshram Oct 26 '15 at 21:53
  • 2
    On Debian / Ubuntu: ``apt-get install mime-support`` – jwhitlock Nov 09 '17 at 18:57