【发布时间】:2016-01-26 02:19:10
【问题描述】:
在 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)
但在 Centos7 中
>>> import mimetypes
>>> mimetypes.guess_type('a.json')
(None, None)
>>> mimetypes.guess_type('a.JSON')
(None, None)
我检查了类似的问题和建议的答案,它只有在给定内容的文件存在时才有效...... How to find the mime type of a file in python?
【问题讨论】:
-
我主要是在猜测,但查看 Python mimetypes 代码,它会查找文件
/etc/mime.types、/etc/httpd/conf/mime.types等,如果存在则读取这些文件。可能你的 Ubuntu 安装有一个映射.json而你的 Centos 安装没有。 -
@torek。谢谢。通过 rpm 包(mailcap)安装 /etc/mime.types 解决了它。
标签: python python-2.7 mime-types centos7