tags:

views:

4846

answers:

6

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.

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

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

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

Does the browser add this information when posting the file to the web page?

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

Edit: Thank you, Dave Webb.

+10  A: 

The minetypes module in the standard library will determine/guess the MIME type from a file extension.

If users are uploading files the HTTP post will contain the MIME type of the file alongside the data. For example, Django makes this data available as an attribute of the UploadedFile object.

Dave Webb
+3  A: 

You didn't state what web server you were using, but Apache has a nice little module called Mime Magic which it uses to determine the type of a file when told to do so. It reads some of the file's content and tries to figure out what type it is based on the characters found. And as Dave Webb Mentioned the MimeTypes Module under python will work, provided an extension is handy.

Alternatively, if you are sitting on a UNIX box you can use sys.popen('file -i ' + fileName, mode='r') to grab the MIME type. Windows should have an equivalent command, but I'm unsure as to what it is.

akdom
+3  A: 

in python 2.6:

mime = subprocess.Popen("/usr/bin/file -i PATH", shell=True, \
    stdout=subprocess.PIPE).communicate()[0]
apito
A: 

how can you find the MIME type in windows ?

Sunil Magesh
check out the accepted solution. The mimetypes module works in windows too.
Daren Thomas
+12  A: 

More reliable way than to use the mimetypes library would be to use the python-magic package.

import magic
m = magic.open(magic.MAGIC_MIME)
m.load()
m.file("/tmp/document.pdf")

This would be equivalent to using file(1).

On Django one could also make sure that the MIME type matches that of UploadedFile.content_type.

toivotuo
Awesome. Thank you.
Noah
See Simon Zimmermann's post for an updated use of python-magic
Daren Thomas
+8  A: 

The python-magic method suggested above is outdated. Python-magic's current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.

# For MIME types
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("testdata/test.pdf")
'application/pdf'
>>>
Simon Zimmermann
thanks for the comment! please note, that "above" is a difficult concept in stackoverflow, since the ordering is grouped by votes and ordered randomly inside the groups. I am guessing you refer to @toivotuo's answer.
Daren Thomas
Yeh, I didn\t have enough "points" to create comments at the time of writing this reply. But I probably should have written it as a comment, so that the @toivotuo could have edited his question.
Simon Zimmermann