guess_type()
mimetypes.guess_type(url, strict=True)
Guess the type of a file based on its filename, path or URL, given by url. URL can be a string or a path-like object.
The return value is a tuple (type, encoding) where type is None if the type can not be guessed (missing or unknown suffix) or a string of the form 'type/subtype', usable for a MIME content-type header. Encoding is None for no encoding or the name of the program used to encode (e.g. compress or gzip).
The encoding is suitable for use as a Content-Encoding header, not as a Content-Transfer-Encoding header. The mappings are table driven. Encoding suffixes are case sensitive; type suffixes are first tried case sensitively, then case insensitively.
NOTE: The strict argument is a flag specifying whether the list of known MIME types is limited to only the official types registered with IANA and is optional.
When strict is True (the default), only the IANA types are supported; When strict is False, some additional non-standard but commonly used MIME types are also recognized.
Changes in version 3.8:
Added support for url being a path-like object.
An example usage:
import mimetypes
print(mimetypes.guess_type("https://docs.python.org/3.8/library/mimetypes.html",strict=True))(text/html, None)
print(mimetypes.guess_type("https://cppsecrets.com",strict=False))(None, None)
#Here, (None, None) represents unknown file type, no encoding repectively for the given URL cppsecrets.com.
NOTE: Green Box shows the Outputs for the LOC(Line Of Code) above it
Comments