mimetypes.guess_type()
mimetypes.guess_type() is a Python function in the Mimetypes Module category. Guesses the MIME type and encoding of a file from its URL/extension. The syntax is mimetypes.guess_type(url, strict=True). Parameters: url — file name/path. Returns: (type, encoding) tuple or None. A typical example: import mimetypes
mimetypes.guess_type("image.jpg") # ("image/jpeg", None)
mimetypes.guess_type("file.html") # ("text/html", None)
mimetypes.guess_type("file.pdf") # ("application/pdf", None). A close sibling is mimetypes.guess_extension(), which guesses the file extension for a given MIME type. A close sibling is mimetypes.init(), which initializes the MIME type database from system files. A close sibling is mimetypes.MimeTypes(), which creates a MIME types registry with optional custom mappings. A close sibling is mimetypes.read_windows_registry(), which loads MIME types from Windows registry (Windows only). More about this category: MIME type guessing — guess_type, guess_extension, init. Related Mimetypes Module entries: mimetypes.guess_extension(), mimetypes.init(), mimetypes.MimeTypes(), mimetypes.read_windows_registry(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.