{"id":3125,"date":"2020-02-09T21:30:22","date_gmt":"2020-02-09T21:30:22","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3125"},"modified":"2020-02-09T21:30:24","modified_gmt":"2020-02-09T21:30:24","slug":"how-to-find-the-mime-type-of-a-file-in-python","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2020\/02\/09\/how-to-find-the-mime-type-of-a-file-in-python\/","title":{"rendered":"How to find the mime type of a file in Python"},"content":{"rendered":"\n<p>A\u00a0<strong>media type<\/strong>\u00a0(also known as a\u00a0<strong>Multipurpose Internet Mail Extensions or MIME type<\/strong>) is a standard that indicates the nature and format of a document, file, or assortment of bytes.\u00a0It is defined and standardized in IETF&rsquo;s\u00a0<a href=\"https:\/\/tools.ietf.org\/html\/rfc6838\">RFC 6838<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"692\" height=\"498\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/02\/ListMimeTypesW.png\" alt=\"\" class=\"wp-image-3126\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/02\/ListMimeTypesW.png 692w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/02\/ListMimeTypesW-300x216.png 300w\" sizes=\"auto, (max-width: 692px) 100vw, 692px\" \/><figcaption>The screenshot showing a list of MIME types. Image courtesy of <a href=\"https:\/\/jhierrot.github.io\/openprodoc\/help\/EN\/ListMimeTypes.html\">https:\/\/jhierrot.github.io\/openprodoc\/help\/EN\/ListMimeTypes.html<\/a><\/figcaption><\/figure>\n\n\n\n<p>Python has a module called <code>mimetypes<\/code> 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,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; import mimetypes\n&gt;&gt;&gt; print(mimetypes.MimeTypes().guess_type('my_file.txt')[0])\ntext\/plain<\/pre>\n\n\n\n<p>You can also use a non-standard module called <code>python-magic<\/code> to deduce the mimetype of a file. For example,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; import magic\n&gt;&gt;&gt; mime = magic.Magic(mime=True)\n&gt;&gt;&gt; mime.from_file(\"my_file.txt\")\ntext\/plain<\/pre>\n\n\n\n<p>See <a href=\"#\">how to detect mime type of a file in Java<\/a> using Apache Tika library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A\u00a0media type\u00a0(also known as a\u00a0Multipurpose Internet Mail Extensions or MIME type) is a standard that indicates the nature and format of a document, file, or assortment of bytes.\u00a0It is defined&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3126,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[196,122],"tags":[687],"class_list":["post-3125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-file-type","category-python","tag-mime-type"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=3125"}],"version-history":[{"count":1,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3125\/revisions"}],"predecessor-version":[{"id":3127,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3125\/revisions\/3127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/3126"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=3125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=3125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=3125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}