How to find the mime type of a file in Python

Print Friendly, PDF & Email

media type (also known as a Multipurpose Internet Mail Extensions or MIME type) is a standard that indicates the nature and format of a document, file, or assortment of bytes. It is defined and standardized in IETF’s RFC 6838.

The screenshot showing a list of MIME types. Image courtesy of

Python has a module called mimetypes that you can use to guess the mime type of a file. However, it is not a reliable way to know the mime type of a file. For example,

>>> import mimetypes
>>> print(mimetypes.MimeTypes().guess_type('my_file.txt')[0])

You can also use a non-standard module called python-magic to deduce the mimetype of a file. For example,

>>> import magic
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("my_file.txt")

See how to detect mime type of a file in Java using Apache Tika library.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *



Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.