Python mimetypes.guess_type














































Python mimetypes.guess_type



guess_type()


mimetypes.guess_type(urlstrict=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


<-PREV                                                                                                                                                NEXT->


More Articles of Shubhanjay Pandey:

Name Views Likes
Python ssl Sockets 1097 3
Python unittest.mock side_effect 603 2
Python ssl Example Server Side Operation 876 3
Python unittest.mock method_calls and mock_calls 1021 2
Python ssl TLS 1.3 and LibreSSL Support 1729 2
Python unittest.mock The patchers 382 2
Python unittest.mock Attaching Mocks as Attributes 511 2
Python ssl Example Client Side Operations 562 3
Python unittest.mock patch.object and patch.dict 3073 2
Python unittest.mock.call_args and call_args_list 4144 2
Python ssl Certificates 492 3
Python unittest.mock Deleting, Mock Names and the name attribute 627 2
Python unittest.mock FILTER_DIR 307 2
Python ssl Introduction 344 3
Python unitest.mock Where to patch, patching descriptors and proxy objects 338 2
Python ssl Memory BIO Support 642 3
Python mimetypes - Introduction 404 3
Python unittest.mock Helpers call 335 2
Python unittest.mock function assert_*() 333 2
Python unittest.mock Introduction 404 2
Python sunau library 354 2
Python unittest.mock The Mock Class 338 2
Python ssl Constants Part 1 444 3
Python mimetypes.add_type 314 4
Python ssl Socket and Context Creation 1430 3
Python unittest.mock.mock_open 432 2
Python ssl non-blocking sockets 733 3
Python unittest.mock MagicMock and magic method support 946 1
Python unittest.mock Magic Mock 432 2
Python ssl Exceptions 376 3
Python unittest.mock TEST_PREFIX and Nesting Patch Decorators 459 2
Python unittest.mock patch.multiple and builtins 2156 2
Python mimetypes additional data 293 3
Python unittest.mock Other HELPERS 311 2
Python MimeTypes Object 314 3
Python mimetypes.guess_type 405 3
Python unittest.mock Autospeccing 393 2
Python unittest.mock.AsyncMock 2765 2
Python ssl Certificates Handling 440 3
Python ssl Random Generation 350 3
Python unittest.mock Function configure_mock 474 2
Python ssl SSLContexts Part 1 743 3
Python unittest.mock patch methods: start and stop 1902 2
Python unittest.mock Calling 321 2
Python unittest.mock other functions 343 2
Python ssl Constants Part 2 512 3
Python ssl SSLContexts Part 2 905 3
Python ssl Security Considerations and SSL Session 479 3
Python mimetypes.guess_extension 407 3
Python unittest.mock PropertyMock and NonCallableMock 927 2
Python mimetypes additional functions 313 3

Comments