code example
python-magic is the python interface to the libmagic file type recognition library. libmagic recognizes file types by checking their headers against a predefined list of file types. This functionality is exposed to the command line via the Unix command file.
>>> import magic >>> magic.from_file("testdata/") 'PDF document, version 1.2' >>> magic.from_buffer(open("testdata/").read(1024)) 'PDF document, version 1.2' >>> magic.from_file("testdata/", mime=True) 'application/pdf'
There is also a Magic class that provides more direct control, including overriding the Magic database file and turning on character encoding detection. This is not recommended for general use. In particular, sharing across multiple threads is not safe and will fail if attempted.
>>> f = (uncompress=True) >>> f.from_file('testdata/') 'ASCII text (gzip compressed data, was "test", last modified: Sat Jun 28 21:32:52 2008, from Unix)'
It can also be combined with the logo option:
>>> f = (mime=True, uncompress=True) >>> f.from_file('testdata/') 'text/plain'
The above this python using magic module for file type recognition method is all I have shared with you, I hope to give you a reference, and I hope you support me more.