How to find the mime type of a file in python?
<p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>

<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) header in the HTTP response.</p>

<p>Based on that assumption, in addition to the bytes of the file, you also want to save the MIME type.</p>

<p>How would you find the MIME type of a file? I'm currently on a Mac, but this should also work on Windows. </p>

<p>Does the browser add this information when posting the file to the web page?</p>

<p>Is there a neat python library for finding this information? A WebService or (even better) a downloadable database?</p>

<p>The python-magic method suggested by toivotuo is outdated. <a href="http://github.com/ahupp/python-magic">Python-magic's</a> current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.</p>

<pre><code># For MIME types
&gt;&gt;&gt; import magic
&gt;&gt;&gt; mime = magic.Magic(mime=True)
&gt;&gt;&gt; mime.from_file("testdata/test.pdf")
'application/pdf'
&gt;&gt;&gt;
</code></pre>
