雖然 Python 內建 mimetypes,但它只是根據副檔名判斷,建議用 python-magic 套件,從檔頭判斷 (背後用 libmagic)。
>>> import magic
>>> magic.from_file('index.html', mime=True)
'text/html'
參考資料:
-
How to find the mime type of a file in python? - Stack Overflow
-
Simon Zimmermann: 用
python-magic,雖然 toivotuo 也是建議用python-magic? 從檔案內容判斷>>> import magic >>> mime = magic.Magic(mime=True) >>> mime.from_file("testdata/test.pdf") 'application/pdf' -
Dave Webb: Python 內建的
mimetypesmodule 則會根據副檔名猜 MIME type;不過 Cerin 認為透過副檔名判斷不太妥當。 -
akdom: Apache 的
mod_mime_magic也會根據檔案內容判斷 content type。 -
mammadori: 有 3 個 library 是包裝 libmagic,有 2 個在 Pip 上可以找到 --
filemagic跟python-magic。
-
-
File Mime types in Django – Andy Byers – Medium (2017-11-23) Python 內建的
mimetypes單從 filename 判斷,可能會失準,建議用python-magic,有file_path_mime(file_path)、check_in_memory_mime(in_memory_file)可用;但什麼是 in-memory file?? -
mimetypes — Map filenames to MIME types — Python 3.7.1 documentation #ril
-
filetype · PyPI #ril
手冊: