mimetypes
--- 將檔案名稱對映到 MIME 類型¶
原始碼:Lib/mimetypes.py
Themimetypes
module converts between a filename or URL and the MIME typeassociated with the filename extension. Conversions are provided from filenameto MIME type and from MIME type to filename extension; encodings are notsupported for the latter conversion.
The module provides one class and a number of convenience functions. Thefunctions are the normal interface to this module, but some applications may beinterested in the class as well.
The functions described below provide the primary interface for this module. Ifthe module has not been initialized, they will callinit()
if they rely onthe informationinit()
sets up.
- mimetypes.guess_type(url,strict=True)¶
Guess the type of a file based on its filename, path or URL, given byurl.URL can be a string or apath-like object.
The return value is a tuple
(type,encoding)
wheretype isNone
if thetype can't be guessed (missing or unknown suffix) or a string of the form'type/subtype'
, usable for a MIMEcontent-type header.encoding is
None
for no encoding or the name of the program used to encode(e.g.compress orgzip). The encoding is suitable for useas aContent-Encoding header,not as aContent-Transfer-Encoding header. The mappings are table driven.Encoding suffixes are case sensitive; type suffixes are first tried casesensitively, then case insensitively.The optionalstrict argument is a flag specifying whether the list of known MIME typesis limited to only the official typesregistered with IANA.Whenstrict is
True
(the default), only the IANA types are supported; whenstrict isFalse
, some additional non-standard but commonly used MIME typesare also recognized.在 3.8 版的變更:Added support forurl being apath-like object.
在 3.13 版之後被棄用:Passing a file path instead of URL issoft deprecated.Use
guess_file_type()
for this.
- mimetypes.guess_file_type(path,*,strict=True)¶
Guess the type of a file based on its path, given bypath.Similar to the
guess_type()
function, but accepts a path instead of URL.Path can be a string, a bytes object or apath-like object.在 3.13 版被加入.
- mimetypes.guess_all_extensions(type,strict=True)¶
Guess the extensions for a file based on its MIME type, given bytype. Thereturn value is a list of strings giving all possible filename extensions,including the leading dot (
'.'
). The extensions are not guaranteed to havebeen associated with any particular data stream, but would be mapped to the MIMEtypetype byguess_type()
andguess_file_type()
.The optionalstrict argument has the same meaning as with the
guess_type()
function.
- mimetypes.guess_extension(type,strict=True)¶
Guess the extension for a file based on its MIME type, given bytype. Thereturn value is a string giving a filename extension, including the leading dot(
'.'
). The extension is not guaranteed to have been associated with anyparticular data stream, but would be mapped to the MIME typetype byguess_type()
andguess_file_type()
.If no extension can be guessed fortype,None
is returned.The optionalstrict argument has the same meaning as with the
guess_type()
function.
Some additional functions and data items are available for controlling thebehavior of the module.
- mimetypes.init(files=None)¶
Initialize the internal data structures. If given,files must be a sequenceof file names which should be used to augment the default type map. If omitted,the file names to use are taken from
knownfiles
; on Windows, thecurrent registry settings are loaded. Each file named infiles orknownfiles
takes precedence over those named before it. Callinginit()
repeatedly is allowed.Specifying an empty list forfiles will prevent the system defaults frombeing applied: only the well-known values will be present from a built-in list.
Iffiles is
None
the internal data structure is completely rebuilt to itsinitial default value. This is a stable operation and will produce the same resultswhen called multiple times.在 3.2 版的變更:Previously, Windows registry settings were ignored.
- mimetypes.read_mime_types(filename)¶
Load the type map given in the filefilename, if it exists. The type map isreturned as a dictionary mapping filename extensions, including the leading dot(
'.'
), to strings of the form'type/subtype'
. If the filefilenamedoes not exist or cannot be read,None
is returned.
- mimetypes.add_type(type,ext,strict=True)¶
Add a mapping from the MIME typetype to the extensionext. When theextension is already known, the new type will replace the old one. When the typeis already known the extension will be added to the list of known extensions.
Whenstrict is
True
(the default), the mapping will be added to theofficial MIME types, otherwise to the non-standard ones.
- mimetypes.inited¶
Flag indicating whether or not the global data structures have been initialized.This is set to
True
byinit()
.
- mimetypes.knownfiles¶
List of type map file names commonly installed. These files are typically named
mime.types
and are installed in different locations by differentpackages.
- mimetypes.suffix_map¶
Dictionary mapping suffixes to suffixes. This is used to allow recognition ofencoded files for which the encoding and the type are indicated by the sameextension. For example, the
.tgz
extension is mapped to.tar.gz
to allow the encoding and type to be recognized separately.
- mimetypes.encodings_map¶
Dictionary mapping filename extensions to encoding types.
- mimetypes.types_map¶
Dictionary mapping filename extensions to MIME types.
- mimetypes.common_types¶
Dictionary mapping filename extensions to non-standard, but commonly found MIMEtypes.
模組的使用範例:
>>>importmimetypes>>>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 物件¶
TheMimeTypes
class may be useful for applications which may want morethan one MIME-type database; it provides an interface similar to the one of themimetypes
module.
- classmimetypes.MimeTypes(filenames=(),strict=True)¶
This class represents a MIME-types database. By default, it provides access tothe same database as the rest of this module. The initial database is a copy ofthat provided by the module, and may be extended by loading additional
mime.types
-style files into the database using theread()
orreadfp()
methods. The mapping dictionaries may also be cleared beforeloading additional data if the default data is not desired.The optionalfilenames parameter can be used to cause additional files to beloaded "on top" of the default database.
- suffix_map¶
Dictionary mapping suffixes to suffixes. This is used to allow recognition ofencoded files for which the encoding and the type are indicated by the sameextension. For example, the
.tgz
extension is mapped to.tar.gz
to allow the encoding and type to be recognized separately. This is initially acopy of the globalsuffix_map
defined in the module.
- encodings_map¶
Dictionary mapping filename extensions to encoding types. This is initially acopy of the global
encodings_map
defined in the module.
- 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 forthe standard types. They are initialized by
common_types
andtypes_map
.
- types_map_inv¶
Tuple containing two dictionaries, mapping MIME types to a list of filenameextensions: the first dictionary is for the non-standards types and thesecond one is for the standard types. They are initialized by
common_types
andtypes_map
.
- guess_extension(type,strict=True)¶
Similar to the
guess_extension()
function, using the tables stored as partof the object.
- guess_type(url,strict=True)¶
Similar to the
guess_type()
function, using the tables stored as part ofthe object.
- guess_file_type(path,*,strict=True)¶
Similar to the
guess_file_type()
function, using the tables storedas part of the object.在 3.13 版被加入.
- guess_all_extensions(type,strict=True)¶
Similar to the
guess_all_extensions()
function, using the tables storedas part of the object.
- read(filename,strict=True)¶
Load MIME information from a file namedfilename. This uses
readfp()
toparse the file.Ifstrict is
True
, information will be added to list of standard types,else to the list of non-standard types.
- readfp(fp,strict=True)¶
Load MIME type information from an open filefp. The file must have the format ofthe standard
mime.types
files.Ifstrict is
True
, information will be added to the list of standardtypes, else to the list of non-standard types.
- read_windows_registry(strict=True)¶
Load MIME type information from the Windows registry.
適用: Windows.
Ifstrict is
True
, information will be added to the list of standardtypes, else to the list of non-standard types.在 3.2 版被加入.
- add_type(type,ext,strict=True)¶
Add a mapping from the MIME typetype to the extensionext. When theextension is already known, the new type will replace the old one. When the typeis already known the extension will be added to the list of known extensions.
Whenstrict is
True
(the default), the mapping will be added to theofficial MIME types, otherwise to the non-standard ones.