all Python 3mimetypesfunction
mimetypes.read_mime_types()
type database
Reads a file in mime.types format and returns an 'extension → type' dictionary, or None if the file could not be opened.
Syntax
mapping = mimetypes.read_mime_types(file)
An open error is not raised but turned into None — check the result before indexing it. The dictionary holds not only the file's lines but the whole built-in Python table, and the function does not change the module database: guess_type will not know the new extensions afterwards. Keys include the dot.
Examples
import mimetypes
with open('my.types', 'w', encoding='utf-8') as f:
f.write('text/x-foo foo\n')
types = mimetypes.read_mime_types('my.types')
print(types['.foo'], types['.html']) # → text/x-foo text/html
print(mimetypes.read_mime_types('missing.types')) # → None
See also
Open in the full glossarydocs.python.orgReport an inaccuracy