【问题标题】:why is mimetypes.guess_type('a.json') not working in centos 7为什么 mimetypes.guess_type('a.json') 在 centos 7 中不起作用
【发布时间】: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


【解决方案1】:

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

yum search mailcap

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

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

>>> import mimetypes
>>> mimetypes.guess_type('a.json')
('application/json', None)

【讨论】:

  • 谢谢。似乎有多个软件包可以在不同的发行版中提供 /etc/mime.types RPM search
  • 在 Debian / Ubuntu 上:apt-get install mime-support
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 2017-10-12
  • 2015-10-28
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多