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
  • 161
  • 1
  • 9
  • 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

2 Answers2

6

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,120
  • 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
0

(A not-so-related answer) On Windows, Mime Types are read from Registry, so you can add mimetypes manually by editing registry entries.

In my case, I developed a python app which receives spread sheets (.xls and .xlsx). And it worked on my dev computer (Windows 10/11), but on production server (Windows Server 2008), .xlsx files are not recognized. mimetypes.guess_type(someXLSXfile.url) printed None.

So I looked up the registry of server, and it turned out that it was too old that it did not have a .xlsx entry. So I added it manually. (Save the code below as xlsx.reg and run it on Windows, the entry then can be added.)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.xlsx]
@="ET.Xlsx.6"
"Content Type"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
"PerceivedType"="document"

If you happen to know the Content Type that is missing, you can fix it by this way. In the code, you should change .xlsx in the square brackets to the extension you want, and for values below, you can copy them from a know computer.

MkSwQi
  • 1
  • 1