在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?
我大多隻是猜測,但看着Python的MIME類型的代碼,它看起來的文件'/等/ mime.types','/等/ httpd/conf/mime.types'等等,如果它們存在的話就讀取它們。可能你的Ubuntu安裝有一個映射'.json',而你的CentOS安裝不會。 – torek
@torek。謝謝。通過rpm包(mailcap)安裝/etc/mime.types解決了它。 – satheeshram