Multipurpose Internet Mail Extensions (MIME) type is a string that is used to identify a type of data that a particular file contains. It's generally used to represents the type of file on the Internet (usually in mails) so that software handling the data can understand how to handle it. The MIME type is a string separated by a slash where the first value is the main type and the second value is the subtype. The emails earlier used to contain only text but it has started supporting attachment with data types like audio, video, XML, pdf, etc. All these data types are stored in files of different formats. The MIME types provide the name which will be used to identify each file type.
Developers many times do not know the MIME type of the file and need it to be determined by itself. Python provides a module named mimetypes that provides a list of methods that has a mapping from file extensions to MIME type and vice-versa. As a part of this tutorial, we'll explain various methods of mimetypes module to find out MIME type based on file URL and vice-versa.
We'll start by importing mimetypes module.
import mimetypes
Below we have explained with few simple examples how we can use guess_type() method to determine the MIME type of the file/URL.
mime_type, encoding = mimetypes.guess_type("https://docs.python.org/3/library/platform.html")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
mime_type, encoding = mimetypes.guess_type("Deploying a Django Application to Google App Engine.pdf")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
mime_type, encoding = mimetypes.guess_type("docs.zip")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
mime_type, encoding = mimetypes.guess_type("brazil_flights_data.csv")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
mime_type, encoding = mimetypes.guess_type("dr_apj_kalam.jpeg")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
mime_type, encoding = mimetypes.guess_type("https://coderzcolumn.com/sitemap.xml")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
mime_type, encoding = mimetypes.guess_type("Videos/snakeviz_1.mp4")
print("MIME Type : {:20s}, Encoding : {}".format(mime_type, encoding))
Below we have first retrieved the MIME type of a few files and URLs and then used it to determine file extensions to explain the usage of guess_extension() method.
mime_type, encoding = mimetypes.guess_type("https://docs.python.org/3/library/platform.html")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("Deploying a Django Application to Google App Engine.pdf")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("docs.zip")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("brazil_flights_data.csv")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("dr_apj_kalam.jpeg")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("https://coderzcolumn.com/sitemap.xml")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("Videos/snakeviz_1.mp4")
extension = mimetypes.guess_extension(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
Our code for this example is almost the same as our previous example with the only difference that we have used guess_all_extensions() method instead.
mime_type, encoding = mimetypes.guess_type("https://docs.python.org/3/library/platform.html")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("Deploying a Django Application to Google App Engine.pdf")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("docs.zip")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("brazil_flights_data.csv")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("dr_apj_kalam.jpeg")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("https://coderzcolumn.com/sitemap.xml")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
mime_type, encoding = mimetypes.guess_type("Videos/snakeviz_1.mp4")
extension = mimetypes.guess_all_extensions(mime_type)
print("MIME Type : {:20s}, Extension : {}".format(mime_type, extension))
print("List of files having MIME mapping details : {}".format(mimetypes.knownfiles))
print("\nMIME Types Suffix Mapping : {}".format(mimetypes.suffix_map))
print("\nExtension to Encoding Mapping : {}".format(mimetypes.encodings_map))
types_mapping = mimetypes.types_map
print("\nMIME Types Mapping : {}".format(list(types_mapping.items())[:10]))
print("\nMIME Type of 3gp : {}".format(types_mapping['.3gp']))
print("MIME Type of 3gp : {}".format(types_mapping['.mp4']))
print("MIME Type of 3gp : {}".format(types_mapping['.pdf']))
print("\nCommon MIME Types Mapping : {}".format(mimetypes.common_types))
mime_types = mimetypes.read_mime_types("/etc/mime.types")
list(mime_types.items())[:10]
mimetypes.add_type("text/json", ".ipynb", strict=True)
types_mapping = mimetypes.types_map
print(".ipynb present in mapping? : {}".format(".ipynb" in types_mapping))
print("\nLast few mappings : {}".format(list(types_mapping.items())[-5:]))
This ends our small tutorial explaining how we can use mimetypes module to determine MIME types from file/URL and vice-versa. Please feel free to let us know your views in the comments section.
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to