Python Glossary Русский

all Python 3mimetypesterm

mimetypes.MimeTypes

type database

A separate MIME type database with its own guess_type, add_type and so on — it leaves the module-wide database alone.

Syntax

db = mimetypes.MimeTypes(filenames=(), strict=True)

The module-level functions share one database, so add_type in one library changes the answers for the whole program. MimeTypes() starts from Python's built-in table and does not read system files such as /etc/mime.types, so its answers are the same on every machine — handy for tests and servers. Load system files explicitly: MimeTypes(['/etc/mime.types']) or the read() method.

Examples

import mimetypes
db = mimetypes.MimeTypes()
db.add_type('text/x-foo', '.foo')
print(db.guess_type('a.foo'))  # → ('text/x-foo', None)
print(mimetypes.guess_type('a.foo'))  # → (None, None) — общая база не тронута

See also

Open in the full glossarydocs.python.orgReport an inaccuracy