📄 module-mimetypes.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>12.11 mimetypes -- Map filenames to MIME types</title>
<META NAME="description" CONTENT="12.11 mimetypes -- Map filenames to MIME types">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-base64.html" tppabs="http://www.python.org/doc/current/lib/module-base64.html">
<LINK REL="previous" href="module-mailcap.html" tppabs="http://www.python.org/doc/current/lib/module-mailcap.html">
<LINK REL="up" href="netdata.html" tppabs="http://www.python.org/doc/current/lib/netdata.html">
<LINK REL="next" href="module-base64.html" tppabs="http://www.python.org/doc/current/lib/module-base64.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-mailcap.html" tppabs="http://www.python.org/doc/current/lib/module-mailcap.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="netdata.html" tppabs="http://www.python.org/doc/current/lib/netdata.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-base64.html" tppabs="http://www.python.org/doc/current/lib/module-base64.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-mailcap.html" tppabs="http://www.python.org/doc/current/lib/module-mailcap.html">12.10 mailcap </A>
<b class="navlabel">Up:</b> <a class="sectref" href="netdata.html" tppabs="http://www.python.org/doc/current/lib/netdata.html">12. Internet Data Handling</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-base64.html" tppabs="http://www.python.org/doc/current/lib/module-base64.html">12.12 base64 </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION00141100000000000000000">
12.11 <tt class="module">mimetypes</tt> --
Map filenames to MIME types</A>
</H1>
<P>
<P>
<P>
The <tt class="module">mimetypes</tt> 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 later conversion.
<P>
The functions described below provide the primary interface for this
module. If the module has not been initialized, they will call
<tt class="function">init()</tt>.
<P>
<dl><dt><b><a name='l2h-2472'><tt class='function'>guess_type</tt></a></b> (<var>filename</var>)
<dd>
Guess the type of a file based on its filename or URL, given by
<var>filename</var>.
The return value is a tuple <code>(<var>type</var>, <var>encoding</var>)</code> where
<var>type</var> is <code>None</code> if the type can't be guessed (no or unknown
suffix) or a string of the form <code>'<var>type</var>/<var>subtype</var>'</code>,
usable for a MIME <code>content-type</code> header; and
encoding is <code>None</code> for no encoding or the name of the program used
to encode (e.g. <b class="program">compress</b> or <b class="program">gzip</b>). The encoding
is suitable for use as a <code>content-encoding</code> header,
<i>not</i> as a <code>content-transfer-encoding</code> header. The mappings
are table driven. Encoding suffixes are case sensitive; type suffixes
are first tried case sensitive, then case insensitive.
</dl>
<P>
<dl><dt><b><a name='l2h-2473'><tt class='function'>guess_extension</tt></a></b> (<var>type</var>)
<dd>
Guess the extension for a file based on its MIME type, given by
<var>type</var>.
The return value is a string giving a filename extension, including the
leading dot ("<tt class="character">.</tt>"). The extension is not guaranteed to have been
associated with any particular data stream, but would be mapped to the
MIME type <var>type</var> by <tt class="function">guess_type()</tt>. If no extension can
be guessed for <var>type</var>, <code>None</code> is returned.
</dl>
<P>
Some additional functions and data items are available for controlling
the behavior of the module.
<P>
<dl><dt><b><a name='l2h-2474'><tt class='function'>init</tt></a></b> (<big>[</big><var>files</var><big>]</big>)
<dd>
Initialize the internal data structures. If given, <var>files</var> must
be a sequence of file names which should be used to augment the
default type map. If omitted, the file names to use are taken from
<code>knownfiles</code>. Each file named in <var>files</var> or
<code>knownfiles</code> takes precedence over those named before it.
Calling <tt class="function">init()</tt> repeatedly is allowed.
</dl>
<P>
<dl><dt><b><a name='l2h-2475'><tt class='function'>read_mime_types</tt></a></b> (<var>filename</var>)
<dd>
Load the type map given in the file <var>filename</var>, if it exists. The
type map is returned as a dictionary mapping filename extensions,
including the leading dot ("<tt class="character">.</tt>"), to strings of the form
<code>'<var>type</var>/<var>subtype</var>'</code>. If the file <var>filename</var> does
not exist or cannot be read, <code>None</code> is returned.
</dl>
<P>
<dl><dt><b><a name='l2h-2476'><tt>inited</tt></a></b>
<dd>
Flag indicating whether or not the global data structures have been
initialized. This is set to true by <tt class="function">init()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-2477'><tt>knownfiles</tt></a></b>
<dd>
List of type map file names commonly installed. These files are
typically named <span class="file">mime.types</span> and are installed in different
locations by different packages.
</dl>
<P>
<dl><dt><b><a name='l2h-2478'><tt>suffix_map</tt></a></b>
<dd>
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 <span class="file">.tgz</span>
extension is mapped to <span class="file">.tar.gz</span> to allow the encoding and type
to be recognized separately.
</dl>
<P>
<dl><dt><b><a name='l2h-2479'><tt>encodings_map</tt></a></b>
<dd>
Dictionary mapping filename extensions to encoding types.
</dl>
<P>
<dl><dt><b><a name='l2h-2480'><tt>types_map</tt></a></b>
<dd>
Dictionary mapping filename extensions to MIME types.
</dl>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-mailcap.html" tppabs="http://www.python.org/doc/current/lib/module-mailcap.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="netdata.html" tppabs="http://www.python.org/doc/current/lib/netdata.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-base64.html" tppabs="http://www.python.org/doc/current/lib/module-base64.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-mailcap.html" tppabs="http://www.python.org/doc/current/lib/module-mailcap.html">12.10 mailcap </A>
<b class="navlabel">Up:</b> <a class="sectref" href="netdata.html" tppabs="http://www.python.org/doc/current/lib/netdata.html">12. Internet Data Handling</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-base64.html" tppabs="http://www.python.org/doc/current/lib/module-base64.html">12.12 base64 </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -