为什么 mimetypes.guess_type('a.json') 在 centos 7 中不起作用

sat*_*ram 2 python python-2.7 mime-types centos7

在 Centos 中,为什么 python 2.7 预建库 mimetypes.guess_type 不返回 json 文件的 mimetype? https://docs.python.org/2/library/mimetypes.html#

我在 mimetypes 中使用 guess_type,它在 centos/ubuntu 中返回不同的值。在不同操作系统中从文件名推断 mimetype 的 pythonic 方法是什么?

在 ubuntu 14.04 中,它返回正确的 mime 类型

>>> import mimetypes
>>> mimetypes.guess_type('a.json')
('application/json', None)
Run Code Online (Sandbox Code Playgroud)

但是在 Centos7

>>> import mimetypes
>>> mimetypes.guess_type('a.json')
(None, None)
>>> mimetypes.guess_type('a.JSON')
(None, None)
Run Code Online (Sandbox Code Playgroud)

我检查了类似的问题和建议的答案,它只有在给定内容的文件存在时才有效...... 如何在 python 中找到文件的 mime 类型?

Sim*_*man 5

在 CentOS 7 上,您需要安装一个名为“mailcap”的软件包:

yum search mailcap
Run Code Online (Sandbox Code Playgroud)

这被描述为“文件类型的帮助程序应用程序和 MIME 类型关联”。

安装mailcap后,以下将起作用:

>>> import mimetypes
>>> mimetypes.guess_type('a.json')
('application/json', None)
Run Code Online (Sandbox Code Playgroud)

  • 在 Debian / Ubuntu 上:``apt-get install mime-support`` (2认同)