2

Possible Duplicate:
How to find the mime type of a file in python?

I want to detect the type of a file, but I don't want to look at the extension because it may be incorrect. The mimetypes module is filename based.

Is there a standard way to peek inside the file?

Community
  • 1
  • 1
Gr1N
  • 1,145
  • 3
  • 13
  • 19
  • Why is it not good? Provide evidence – Andreas Jung Aug 11 '12 at 18:50
  • 1
    I don't want to use mimetypes module because this module look only for file path. For example name of file test.pdf but this is .doc file with fake name. – Gr1N Aug 11 '12 at 19:01
  • 1
    NOT A DUPLICATE QUESTION -- the referenced answer is looking at file name not file content – Alvin Feb 16 '16 at 18:46
  • Check my answer in the context of no file extension or false file extension, Python 3.X and web application http://stackoverflow.com/a/39356849/1209842 – Claude COULOMBE Sep 06 '16 at 20:03

1 Answers1

7

Try installing the python-magic module.

 >>> mime = magic.Magic(mime=True)
 >>> mime.from_file("testdata/abc.pdf")
'application/pdf'
msw
  • 40,500
  • 8
  • 77
  • 106
NIlesh Sharma
  • 4,997
  • 6
  • 30
  • 50
  • +1 good answer, but I had to hunt for it and the documentation is um... brief. It probably only works on unixes too, but I'm not sure. – msw Aug 11 '12 at 20:49
  • This is good module, but I want to use only standart library. – Gr1N Aug 12 '12 at 08:33
  • Check my answer in the context of no file extension or false file extension, Python 3.X and web application http://stackoverflow.com/a/39356849/1209842 – Claude COULOMBE Sep 06 '16 at 20:05