How to check type of files without extensions? How to check type of files without extensions? python python

How to check type of files without extensions?


There are Python libraries that can recognize files based on their content (usually a header / magic number) and that don't rely on the file name or extension.

If you're addressing many different file types, you can use python-magic. That's just a Python binding for the well-established magic library. This has a good reputation and (small endorsement) in the limited use I've made of it, it has been solid.

There are also libraries for more specialized file types. For example, the Python standard library has the imghdr module that does the same thing just for image file types.

If you need dependency-free (pure Python) file type checking, see filetype.


The Python Magic library provides the functionality you need.

You can install the library with pip install python-magic and use it as follows:

>>> import magic>>> magic.from_file('iceland.jpg')'JPEG image data, JFIF standard 1.01'>>> magic.from_file('iceland.jpg', mime=True)'image/jpeg'>>> magic.from_file('greenland.png')'PNG image data, 600 x 1000, 8-bit colormap, non-interlaced'>>> magic.from_file('greenland.png', mime=True)'image/png'

The Python code in this case is calling to libmagic beneath the hood, which is the same library used by the *NIX file command. Thus, this does the same thing as the subprocess/shell-based answers, but without that overhead.


On unix and linux there is the file command to guess file types. There's even a windows port.

From the man page:

File tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be printed.

You would need to run the file command with the subprocess module and then parse the results to figure out an extension.

edit: Ignore my answer. Use Chris Johnson's answer instead.