4

In linux, we have a utility called “file”, which helps us to determine the identification of a file. Is there any python module that can do the same job?

I don't prefer to use subprocess.Popen(['file', 'blah.blah']), because it is platform dependent. For instance, windows do not have “file” (although it can be downloaded).

Hanxue
  • 12,243
  • 18
  • 88
  • 130
prgbenz
  • 1,129
  • 4
  • 13
  • 27
  • 1
    See also http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python – utapyngo Oct 12 '11 at 07:22

3 Answers3

7

magic

Don't forget the DLL.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Ignacio mentioned the Magic library, but you could also do it with the standard library if you're confident that the filename is enough to know the file type from (by extension):

>>> import mimetypes
>>> mimetypes.guess_type('__init__.py')
('text/x-python', None)

See the Mimetypes module in the standard library. This however isn't a replacement for actually inspecting the content of the file and figuring out its type.

Ken Kinder
  • 12,654
  • 6
  • 50
  • 70
  • 2
    No, **please** don't see that. "The `mimetypes` module converts between a filename or URL and the MIME type associated with the **filename extension**." – Ignacio Vazquez-Abrams Oct 12 '11 at 07:40
  • Oh, that's a good point. If you want it from the content of the file, guess_type isn't what you want. You're right, I'll update my answer. – Ken Kinder Oct 12 '11 at 21:02
0

There are standard modules imghdr and sndhdr for graphic and sound files, respectively.

refaim
  • 1,875
  • 1
  • 14
  • 9