2015-10-26 39 views
2

在Centos中,爲什麼python 2.7預建庫mimetypes.guess_type不能返回mimetype用於json文件? https://docs.python.org/2/library/mimetypes.html#爲什麼mimetypes.guess_type('a.json')不能在centos中運行7

我在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?

+0

我大多隻是猜測,但看着Python的MIME類型的代碼,它看起來的文件'/等/ mime.types','/等/ httpd/conf/mime.types'等等,如果它們存在的話就讀取它們。可能你的Ubuntu安裝有一個映射'.json',而你的CentOS安裝不會。 – torek

+0

@torek。謝謝。通過rpm包(mailcap)安裝/etc/mime.types解決了它。 – satheeshram

回答

3

在CentOS 7上,您需要安裝名爲「mailcap」的軟件包:

yum search mailcap 

這被描述爲「文件類型的助手應用程序和MIME類型關聯」。

mailcap的安裝後,下面的工作:

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

謝謝。似乎有多個軟件包可以在不同的發行版中提供/etc/mime.types [RPM搜索](http://rpmfind.net/linux/rpm2html/search.php?query=%2Fetc%2Fmime.types) – satheeshram

+0

On Debian/Ubuntu:''apt-get install mime-support'' – jwhitlock

相關問題