Find the mime type of a file.

Discussion in 'Python' started by Olive, Jan 25, 2012.

  1. Olive

    Olive Guest

    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
     
    Olive, Jan 25, 2012
    #1
    1. Advertising

  2. Olive

    Chris Rebert Guest

    On Wed, Jan 25, 2012 at 9:04 AM, Olive <> 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
     
    Chris Rebert, Jan 25, 2012
    #2
    1. Advertising

  3. Olive

    Jon Clements Guest

    On Jan 25, 5:04 pm, Olive <> 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.
     
    Jon Clements, Jan 26, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Riek
    Replies:
    0
    Views:
    4,455
    Stephen Riek
    Sep 19, 2003
  2. CJ
    Replies:
    1
    Views:
    1,586
    Andrew Thompson
    Oct 29, 2004
  3. Jan Arickx
    Replies:
    0
    Views:
    209
    Jan Arickx
    Aug 25, 2003
  4. joe
    Replies:
    0
    Views:
    210
  5. ecureuil
    Replies:
    0
    Views:
    342
    ecureuil
    May 28, 2006
Loading...

Share This Page