Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19407 > unrolled thread

Find the mime type of a file.

Started byOlive <diolu@bigfoot.com>
First post2012-01-25 18:04 +0100
Last post2012-01-25 16:12 -0800
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Find the mime type of a file. Olive <diolu@bigfoot.com> - 2012-01-25 18:04 +0100
    Re: Find the mime type of a file. Chris Rebert <clp2@rebertia.com> - 2012-01-25 09:40 -0800
    Re: Find the mime type of a file. Jon Clements <joncle@googlemail.com> - 2012-01-25 16:12 -0800

#19407 — Find the mime type of a file.

FromOlive <diolu@bigfoot.com>
Date2012-01-25 18:04 +0100
SubjectFind the mime type of a file.
Message-ID<20120125180432.76079215@bigfoot.com>
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

[toc] | [next] | [standalone]


#19414

FromChris Rebert <clp2@rebertia.com>
Date2012-01-25 09:40 -0800
Message-ID<mailman.5082.1327513254.27778.python-list@python.org>
In reply to#19407
On Wed, Jan 25, 2012 at 9:04 AM, Olive <diolu@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

[toc] | [prev] | [next] | [standalone]


#19451

FromJon Clements <joncle@googlemail.com>
Date2012-01-25 16:12 -0800
Message-ID<ab5cb77b-8efa-455d-a79d-ba0f897a5a90@n6g2000vbz.googlegroups.com>
In reply to#19407
On Jan 25, 5:04 pm, Olive <di...@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).
>
> 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 = Image.open(fname)
    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).

hth,

Jon.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web