本文介绍了为什么mimetypes.guess_type('a.json')在centos 7中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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#

我在mimetypes中使用guess_type,它在centos/ubuntu中返回不同的值.从不同操作系统的文件名中推断出模仿类型的pythonic方式是什么?

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?

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

In ubuntu 14.04, it returns the correct mime type

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

但是在Centos7中

But in Centos7

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

我检查了类似的问题并提出了建议的答案,只有在给定内容的文件存在的情况下它才起作用...如何在以下位置查找文件的mime类型python?

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?

推荐答案

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

On CentOS 7 you will need to install a package named "mailcap":

yum search mailcap

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

This is described as "Helper application and MIME type associations for file types".

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

After installing mailcap, the following will work:

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

这篇关于为什么mimetypes.guess_type('a.json')在centos 7中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:27