summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>1998-05-18 16:27:20 (GMT)
committerFred Drake <fdrake@acm.org>1998-05-18 16:27:20 (GMT)
commit5109ffd607b58237a35fcb364a9c6698ab511cde (patch)
tree0c67ace3182dafc99f12fe8a96f1ecec28a89845 /Lib
parent67133e25a28ad2d1117fb976499b67f5955f877a (diff)
downloadcpython-5109ffd607b58237a35fcb364a9c6698ab511cde.zip
cpython-5109ffd607b58237a35fcb364a9c6698ab511cde.tar.gz
cpython-5109ffd607b58237a35fcb364a9c6698ab511cde.tar.bz2
guess_extension(): New function. Performs a reverse mapping from MIME type
to filename extension.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/mimetypes.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index bde0ec9..6bc55ea 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -1,9 +1,11 @@
"""Guess the MIME type of a file.
-This module defines one useful function:
+This module defines two useful functions:
guess_type(url) -- guess the MIME type and encoding of a URL.
+guess_extension(type) -- guess the extension for a given MIME type.
+
It also contains the following, for tuning the behavior:
Data:
@@ -64,6 +66,21 @@ def guess_type(url):
else:
return None, encoding
+def guess_extension(type):
+ """Guess the extension for a file based on its MIME type.
+
+ 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 has been known to be
+ used for streams of the MIME type given by `type'. If `type' is not
+ known, None is returned.
+ """
+ type = string.lower(type)
+ for ext, stype in types_map.items():
+ if type == stype:
+ return ext
+ return None
+
def init(files=None):
global inited
for file in files or knownfiles: