On this page
mimetypes.MimeTypes
Prev: mimetypes.init | Up: mimetypes | Next: mimetypes.read_mime_types
Rel: mimetypes.add_type, mimetypes.guess_all_extensions, mimetypes.guess_extension, mimetypes.guess_file_type
Ref: docs.python.org — mimetypes.MimeTypes
Tags: #public #python3 #stdlib #mimetypes
Import
import mimetypes
from mimetypes import MimeTypes
Signature & Overview
mimetypes.MimeTypes(filenames=(), strict=True)
MIME-types datastore.
Methods & Attributes
MimeTypes.add_type(self, type, ext, strict=True)— Add a mapping between a type and an extension.MimeTypes.guess_all_extensions(self, type, strict=True)— Guess the extensions for a file based on its MIME type.MimeTypes.guess_extension(self, type, strict=True)— Guess the extension for a file based on its MIME type.MimeTypes.guess_file_type(self, path, *, strict=True)— Guess the type of a file based on its path.MimeTypes.guess_type(self, url, strict=True)— Guess the type of a file which is either a URL or a path-like object.MimeTypes.read(self, filename, strict=True)— Read a single mime.types-format file, specified by pathname.MimeTypes.read_windows_registry(self, strict=True)— Load the MIME types database from Windows registry.MimeTypes.readfp(self, fp, strict=True)— Read a single mime.types-format file.
Usage
import mimetypes
db = mimetypes.MimeTypes()
db.add_type("application/x-demo", ".demo")
assert db.guess_type("file.demo")[0] == "application/x-demo"
Gotchas
- Isolated registry instance: Prefer
MimeTypes()overmimetypes.add_type()when you don't want to mutate global state.
REPL Inspect
from mimetypes import MimeTypes
help(MimeTypes)
Linked from
- tuts/python3/stdlib_module/mimetypes
- tuts/python3/stdlib_module/mimetypes/add_type
- tuts/python3/stdlib_module/mimetypes/guess_all_extensions
- tuts/python3/stdlib_module/mimetypes/guess_extension
- tuts/python3/stdlib_module/mimetypes/guess_file_type
- tuts/python3/stdlib_module/mimetypes/guess_type
- tuts/python3/stdlib_module/mimetypes/init
- tuts/python3/stdlib_module/mimetypes/MimeTypes
- tuts/python3/stdlib_module/mimetypes/read_mime_types
Related notes
- tuts/python3/stdlib_module/mimetypes/add_type Linked here; links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes/guess_all_extensions Linked here; links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes/guess_extension Linked here; links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes/guess_file_type Linked here; links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes/init Linked here; links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes/read_mime_types Linked here; links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes/guess_type links to this page; shared tags: #mimetypes, #stdlib
- tuts/python3/stdlib_module/mimetypes links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib