Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Find the mime type of a file.

Reply
Thread Tools

Find the mime type of a file.

 
 
Olive
Guest
Posts: n/a
 
      01-25-2012
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

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-25-2012
On Wed, Jan 25, 2012 at 9:04 AM, Olive <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      01-26-2012
On Jan 25, 5:04*pm, Olive <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Email::MIME and Email::MIME::Attachment::Stripper ecureuil Perl Misc 0 05-28-2006 01:47 AM
tomcat 4.x : setting mime type for a directory or setting a default mime type CJ Java 1 10-29-2004 07:51 PM
how 2 convert mime to txt or extract words from mime joe Perl Misc 0 04-07-2004 12:34 AM
JSP upload - detecting file type by mime type? what is application/octet-stream? Stephen Riek Java 0 09-19-2003 03:52 AM
Receiving zip files via Mime::Parser/Mime::Decoder Jan Arickx Perl Misc 0 08-25-2003 08:24 PM



Advertisments