File Format Definition

In linux, we have a utility called β€œfile” that helps us determine the identity of the file. Is there any python module that can do the same job?

I do not prefer to use subprocess.Popen(['file', 'blah.blah']) because it is platform dependent. For example, windows do not have a β€œfile” (although it can be downloaded).

+4
source share
3 answers

Ignacio mentioned the Magic library, but you can also do this with the standard library if you are sure that the file name is enough to find out the file type (by extension):

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

See the Mimetypes module in the standard library. This, however, is not a substitute for actually controlling the contents of a file and determining its type.

+2
source

magic

Do not forget the DLL .

+7
source

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

0
source

All Articles