Find the mime type of a file.

Chris Rebert clp2 at rebertia.com
Wed Jan 25 12:40:47 EST 2012


On Wed, Jan 25, 2012 at 9:04 AM, Olive <diolu at bigfoot.com> 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).

There's 3rd-party Python bindings for the library that underlies the
`file` command:
https://github.com/ahupp/python-magic
And there's an unrelated pure(?) Python standalone module from A-A-P:
http://www.a-a-p.org/exec/ref-filetype.html

Tip: google "file type detection python"

Cheers,
Chris



More information about the Python-list mailing list