mimetypes
— 将文件名映射到 MIME 类型
¶
源代码: Lib/mimetypes.py
The
mimetypes
module converts between a filename or URL and the MIME type associated with the filename extension. Conversions are provided from filename to MIME type and from MIME type to filename extension; encodings are not supported for the latter conversion.
The module provides one class and a number of convenience functions. The functions are the normal interface to this module, but some applications may be interested in the class as well.
The functions described below provide the primary interface for this module. If the module has not been initialized, they will call
init()
if they rely on the information
init()
sets up.
- mimetypes. guess_type ( url , strict = True ) ¶
-
Guess the type of a file based on its filename, path or URL, given by url . URL can be a string or a 像路径对象 .
The return value is a tuple
(type, encoding)where type isNoneif the type can’t be guessed (missing or unknown suffix) or a string of the form'type/subtype', usable for a MIME content-type 头。encoding is
Nonefor no encoding or the name of the program used to encode (e.g. compress or gzip ). The encoding is suitable for use as a Content-Encoding header, not 作为 Content-Transfer-Encoding header. The mappings are table driven. Encoding suffixes are case sensitive; type suffixes are first tried case sensitively, then case insensitively.可选 strict argument is a flag specifying whether the list of known MIME types is limited to only the official types registered with IANA 。当 strict is
True(the default), only the IANA types are supported; when strict isFalse, some additional non-standard but commonly used MIME types are also recognized.3.8 版改变: 添加支持 url being a 像路径对象 .