Find the mime type of a file.

Jon Clements joncle at
Wed Jan 25 19:12:40 EST 2012

On Jan 25, 5:04 pm, Olive <di... at> wrote:
> I want to have a list of all the images in a directory. To do so I want
> to have a function that find the mime type of a file. I have found
> mimetypes.guess_type but it only works by examining the extension. In
> GNU/Linux the "file" utility do much better by actually looking at the
> file. Is there an equivalent function in python (as a last resort I can
> always use the external file utility).
> Olive

You could also try using PIL.(I hardly use it, but...)

from PIL import Image
for fname in [some list of filenames here]:
    img =
    print img.format

Might be more expensive than the file utility, but that's up to you to
determine (open might be lazy, or it might load it - there is a
separate load function though, so who knows).



More information about the Python-list mailing list