mimetypesMap filenames to MIME types将文件名映射到MIME类型

Source code: Lib/mimetypes.py


The mimetypes module converts between a filename or URL and the MIME type associated with the filename extension. mimetypes模块在文件名或URL与与文件扩展名关联的MIME类型之间进行转换。Conversions are provided from filename to MIME type and from MIME type to filename extension; encodings are not supported for the latter conversion.提供从文件名到MIME类型以及从MIME类型到文件扩展名的转换;后一转换不支持编码。

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.如果模块尚未初始化,如果依赖init()设置的信息,则将调用init()

mimetypes.guess_type(url, strict=True)

Guess the type of a file based on its filename, path or URL, given by url. 根据url给出的文件名、路径或URL猜测文件的类型。URL can be a string or a path-like object.URL可以是字符串或类路径对象

The return value is a tuple (type, encoding) where type is None if the type can’t be guessed (missing or unknown suffix) or a string of the form 'type/subtype', usable for a MIME content-type header.返回值是一个(type, encoding),其中如果无法猜测类型(缺少或未知后缀),则类型为None,或者是'type/subtype'形式的字符串,可用于MIMEcontent-type标头。

encoding is None for no encoding or the name of the program used to encode (e.g. compress or gzip). encodingNone,表示没有编码或用于编码的程序名称(例如compressgzip)。The encoding is suitable for use as a Content-Encoding header, not as a Content-Transfer-Encoding header. 该编码适合用作Content-Encoding标头,而适合用作Content-Transfer-Encoding标头。The mappings are table driven. 映射是表驱动的。Encoding suffixes are case sensitive; type suffixes are first tried case sensitively, then case insensitively.编码后缀区分大小写;类型后缀首先区分大小写,然后不区分大小写。

The optional strict argument is a flag specifying whether the list of known MIME types is limited to only the official types registered with IANA. 可选的strict参数是一个标志,指定已知MIME类型的列表是否仅限于向IANA注册的正式类型。When strict is True (the default), only the IANA types are supported; when strict is False, some additional non-standard but commonly used MIME types are also recognized.strictTrue(默认值)时,仅支持IANA类型;当strictFalse时,还会识别一些其他非标准但常用的MIME类型。

Changed in version 3.8:版本3.8中更改: Added support for url being a path-like object.添加了对url作为类路径对象的支持。

mimetypes.guess_all_extensions(type, strict=True)

Guess the extensions for a file based on its MIME type, given by type. 根据文件的MIME类型(按type给出)猜测文件的扩展名。The return value is a list of strings giving all possible filename extensions, including the leading dot ('.'). 返回值是一个字符串列表,列出所有可能的文件扩展名,包括前导点('.')。The extensions are not guaranteed to have been associated with any particular data stream, but would be mapped to the MIME type type by guess_type().这些扩展不保证与任何特定的数据流相关联,但会通过guess_type()映射到MIME类型type

The optional strict argument has the same meaning as with the guess_type() function.可选的strict参数与guess_type()函数的含义相同。

mimetypes.guess_extension(type, strict=True)

Guess the extension for a file based on its MIME type, given by type. 根据文件的MIME类型(按type给出)猜测文件的扩展名。The return value is a string giving a filename extension, including the leading dot ('.'). 返回值是一个提供文件扩展名的字符串,包括前导点('.')。The extension is not guaranteed to have been associated with any particular data stream, but would be mapped to the MIME type type by guess_type(). 扩展不保证与任何特定的数据流相关联,但会通过guess_type()映射到MIME类型typeIf no extension can be guessed for type, None is returned.如果无法猜测type的扩展名,则返回None

The optional strict argument has the same meaning as with the guess_type() function.可选的strict参数与guess_type()函数的含义相同。

Some additional functions and data items are available for controlling the behavior of the module.一些附加功能和数据项可用于控制模块的行为。

mimetypes.init(files=None)

Initialize the internal data structures. 初始化内部数据结构。If given, files must be a sequence of file names which should be used to augment the default type map. 如果给定,files必须是一系列文件名,这些文件名应用于增强默认类型映射。If omitted, the file names to use are taken from knownfiles; on Windows, the current registry settings are loaded. 如果省略,则使用的文件名取自knownfiles;在Windows上,加载当前注册表设置。Each file named in files or knownfiles takes precedence over those named before it. filesknownfiles中命名的每个文件优先于在其之前命名的文件。Calling init() repeatedly is allowed.允许重复调用init()

Specifying an empty list for files will prevent the system defaults from being applied: only the well-known values will be present from a built-in list.为文件指定一个空列表将阻止应用系统默认值:内置列表中将只显示已知值。

If files is None the internal data structure is completely rebuilt to its initial default value. 如果filesNone,则内部数据结构将完全重建为其初始默认值。This is a stable operation and will produce the same results when called multiple times.这是一个稳定的操作,当多次调用时会产生相同的结果。

Changed in version 3.2:版本3.2中更改: Previously, Windows registry settings were ignored.以前,Windows注册表设置被忽略。

mimetypes.read_mime_types(filename)

Load the type map given in the file filename, if it exists. 加载文件filename中给定的类型映射(如果存在)。The type map is returned as a dictionary mapping filename extensions, including the leading dot ('.'), to strings of the form 'type/subtype'. 类型映射作为字典返回,将文件扩展名(包括前导点(“'.'))映射为'type/subtype'形式的字符串。If the file filename does not exist or cannot be read, None is returned.如果文件filename不存在或无法读取,则返回None

mimetypes.add_type(type, ext, strict=True)

Add a mapping from the MIME type type to the extension ext. 添加从MIMEtype到扩展名extWhen the extension is already known, the new type will replace the old one. 当扩展名已知时,新类型将替换旧类型。When the type is already known the extension will be added to the list of known extensions.当类型已知时,扩展名将添加到已知扩展名列表中。

When strict is True (the default), the mapping will be added to the official MIME types, otherwise to the non-standard ones.strictTrue(默认值)时,映射将添加到正式MIME类型,否则添加到非标准MIME类型。

mimetypes.inited

Flag indicating whether or not the global data structures have been initialized. 指示全局数据结构是否已初始化的标志。This is set to True by init().init()将其设置为True

mimetypes.knownfiles

List of type map file names commonly installed. 通常安装的类型映射文件名列表。These files are typically named mime.types and are installed in different locations by different packages.这些文件通常命名为mime.types,并由不同的软件包安装在不同的位置。

mimetypes.suffix_map

Dictionary mapping suffixes to suffixes. 将后缀映射到后缀的字典。This is used to allow recognition of encoded files for which the encoding and the type are indicated by the same extension. 这用于识别编码和类型由相同扩展名表示的编码文件。For example, the .tgz extension is mapped to .tar.gz to allow the encoding and type to be recognized separately.例如,.tgz扩展名被映射到.tar.gz,以允许单独识别编码和类型。

mimetypes.encodings_map

Dictionary mapping filename extensions to encoding types.将文件扩展名映射到编码类型的字典。

mimetypes.types_map

Dictionary mapping filename extensions to MIME types.将文件扩展名映射到MIME类型的字典。

mimetypes.common_types

Dictionary mapping filename extensions to non-standard, but commonly found MIME types.将文件扩展名映射到非标准但常见的MIME类型的字典。

An example usage of the module:模块的用法示例:

>>> import mimetypes
>>> mimetypes.init()
>>> mimetypes.knownfiles
['/etc/mime.types', '/etc/httpd/mime.types', ... ]
>>> mimetypes.suffix_map['.tgz']
'.tar.gz'
>>> mimetypes.encodings_map['.gz']
'gzip'
>>> mimetypes.types_map['.tgz']
'application/x-tar-gz'

MimeTypes ObjectsMimeTypes对象

The MimeTypes class may be useful for applications which may want more than one MIME-type database; it provides an interface similar to the one of the mimetypes module.MimeTypes类可能对需要多个MIME类型数据库的应用程序有用;它提供了一个类似于mimetypes模块的接口。

classmimetypes.MimeTypes(filenames=(), strict=True)

This class represents a MIME-types database. 此类表示MIME类型数据库。By default, it provides access to the same database as the rest of this module. 默认情况下,它提供对与此模块其余部分相同的数据库的访问。The initial database is a copy of that provided by the module, and may be extended by loading additional mime.types-style files into the database using the read() or readfp() methods. 初始数据库是模块提供的数据库的副本,可以通过使用read()readfp()方法将其他mime.types样式文件加载到数据库中来扩展。The mapping dictionaries may also be cleared before loading additional data if the default data is not desired.如果不需要默认数据,也可以在加载附加数据之前清除映射字典。

The optional filenames parameter can be used to cause additional files to be loaded “on top” of the default database.可选的filenames参数可用于在默认数据库的“顶部”加载其他文件。

suffix_map

Dictionary mapping suffixes to suffixes. 将后缀映射到后缀的字典。This is used to allow recognition of encoded files for which the encoding and the type are indicated by the same extension. 这用于识别编码和类型由相同扩展名表示的编码文件。For example, the .tgz extension is mapped to .tar.gz to allow the encoding and type to be recognized separately. 例如,.tgz扩展名被映射到.tar.gz,以允许单独识别编码和类型。This is initially a copy of the global suffix_map defined in the module.这最初是模块中定义的全局suffix_map的副本。

encodings_map

Dictionary mapping filename extensions to encoding types. 将文件扩展名映射到编码类型的字典。This is initially a copy of the global encodings_map defined in the module.这最初是模块中定义的全局encodings_map的副本。

types_map

Tuple containing two dictionaries, mapping filename extensions to MIME types: the first dictionary is for the non-standards types and the second one is for the standard types. 元组包含两个字典,将文件扩展名映射到MIME类型:第一个字典用于非标准类型,第二个字典用于标准类型。They are initialized by common_types and types_map.它们由common_typestypes_map初始化。

types_map_inv

Tuple containing two dictionaries, mapping MIME types to a list of filename extensions: the first dictionary is for the non-standards types and the second one is for the standard types. 元组包含两个字典,将MIME类型映射到文件扩展名列表:第一个字典用于非标准类型,第二个字典用于标准类型。They are initialized by common_types and types_map.它们由common_typestypes_map初始化。

guess_extension(type, strict=True)

Similar to the guess_extension() function, using the tables stored as part of the object.类似于guess_extension()函数,使用作为对象一部分存储的表。

guess_type(url, strict=True)

Similar to the guess_type() function, using the tables stored as part of the object.类似于guess_type()函数,使用作为对象一部分存储的表。

guess_all_extensions(type, strict=True)

Similar to the guess_all_extensions() function, using the tables stored as part of the object.类似于guess_all_extensions()函数,使用作为对象一部分存储的表。

read(filename, strict=True)

Load MIME information from a file named filename. 从名为filename的文件加载MIME信息。This uses readfp() to parse the file.这使用readfp()来解析文件。

If strict is True, information will be added to list of standard types, else to the list of non-standard types.如果strictTrue,则信息将添加到标准类型列表,否则添加到非标准类型列表。

readfp(fp, strict=True)

Load MIME type information from an open file fp. 从打开的文件fp加载MIME类型信息。The file must have the format of the standard mime.types files.文件必须具有标准mime.types文件的格式。

If strict is True, information will be added to the list of standard types, else to the list of non-standard types.如果strictTrue,则信息将添加到标准类型列表,否则添加到非标准类型列表。

read_windows_registry(strict=True)

Load MIME type information from the Windows registry.从Windows注册表加载MIME类型信息。

Availability: Windows.

If strict is True, information will be added to the list of standard types, else to the list of non-standard types.如果strictTrue,则信息将添加到标准类型列表,否则添加到非标准类型列表。

New in version 3.2.版本3.2中新增。