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

故事扮演 提交于 2019-12-12 17:30:18

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/33354969/why-is-mimetypes-guess-typea-json-not-working-in-centos-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!