On this page
mimetypes
Prev: math | Up: stdlib_module | Next: mmap
Rel: email, json, mailbox, base64, binascii
Ref: docs.python.org — mimetypes
Tags: #public #python3 #stdlib
Import
import mimetypes
Overview
Maps filename extensions to IANA MIME types (guess_type, guess_file_type, guess_extension, add_type). Essential for HTTP static file servers, email attachments, and upload handlers needing accurate Content-Type headers without hardcoding extension tables.
Quick Start
import mimetypes
mime, encoding = mimetypes.guess_type("report.json")
assert mime == "application/json" and encoding is None
ext = mimetypes.guess_extension("text/markdown")
assert ext in {".md", ".markdown"}
Implementation Walkthrough
1. Computing Safe Content-Type Headers with Fallback
import mimetypes
from pathlib import Path
def content_type_for(path: Path) -> str:
mime, _ = mimetypes.guess_type(path.name)
if mime is None:
return "application/octet-stream"
if mime.startswith("text/") or mime in {"application/json", "application/javascript"}:
return f"{mime}; charset=utf-8"
return mime
assert content_type_for(Path("style.css")) == "text/css; charset=utf-8"
assert content_type_for(Path("blob.unknownext")) == "application/octet-stream"
Gotchas & Boundaries
- Guesses strictly by filename extension, NOT file contents: Never trust
guess_type()alone for security validation of user uploads. - OS mime registries differ: Register custom types explicitly (
mimetypes.add_type("text/markdown", ".md")) or use an isolatedmimetypes.MimeTypes()instance for deterministic cross-platform behavior. - Python 3.13+ adds
guess_file_type(path)specifically for filesystem paths (whereasguess_type()expects a URL or filename).
Common Functions
- mimetypes.add_type() —
add_type(type, ext, strict=True)— Add a mapping between a type and an extension. - mimetypes.guess_all_extensions() —
guess_all_extensions(type, strict=True)— Guess the extensions for a file based on its MIME type. - mimetypes.guess_extension() —
guess_extension(type, strict=True)— Guess the extension for a file based on its MIME type. - mimetypes.guess_file_type() —
guess_file_type(path, *, strict=True)— Guess the type of a file based on its path. - mimetypes.guess_type() —
guess_type(url, strict=True)— Guess the type of a file based on its URL. - mimetypes.init() —
init(files=None)—mimetypes.init()function. - mimetypes.read_mime_types() —
read_mime_types(file)—mimetypes.read_mime_types()function.
Classes & Objects
- mimetypes.MimeTypes — MIME-types datastore. (Methods:
.add_type(),.guess_all_extensions(),.guess_extension(),.guess_file_type(),.guess_type(),.read())
Constants & Attributes
mimetypes.common_types(dict) ={'.rtf': 'application/rtf', '.apk': 'application/vnd.andr...mimetypes.encodings_map(dict) ={'.gz': 'gzip', '.Z': 'compress', '.bz2': 'bzip2', '.xz':...mimetypes.inited(bool) =Falsemimetypes.knownfiles(list) =['/etc/mime.types', '/etc/httpd/mime.types', '/etc/httpd/...mimetypes.suffix_map(dict) ={'.svgz': '.svg.gz', '.tgz': '.tar.gz', '.taz': '.tar.gz'...mimetypes.types_map(dict) ={'.js': 'text/javascript', '.mjs': 'text/javascript', '.e...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/base64
- tuts/python3/stdlib_module/binascii
- tuts/python3/stdlib_module/email
- tuts/python3/stdlib_module/ftplib
- tuts/python3/stdlib_module/html
- tuts/python3/stdlib_module/http
- tuts/python3/stdlib_module/imaplib
- tuts/python3/stdlib_module/ipaddress
- tuts/python3/stdlib_module/json
- tuts/python3/stdlib_module/mailbox
- tuts/python3/stdlib_module/math
- tuts/python3/stdlib_module/mmap
- tuts/python3/stdlib_module/nturl2path
- tuts/python3/stdlib_module/poplib
- tuts/python3/stdlib_module/quopri
- tuts/python3/stdlib_module/smtplib
- tuts/python3/stdlib_module/socketserver
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/urllib
- tuts/python3/stdlib_module/uuid
- tuts/python3/stdlib_module/webbrowser
- tuts/python3/stdlib_module/wsgiref
- tuts/python3/stdlib_module/xml
- tuts/python3/stdlib_module/xmlrpc
Related notes
- tuts/python3/stdlib_module/base64 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/email Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/mmap Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ftplib links to this page; shared tags: #stdlib