h2non / filetype.py

Small, dependency-free, fast Python package to infer binary file types checking the magic numbers signature
https://h2non.github.io/filetype.py
MIT License
627 stars 109 forks source link
extension filetype inference magic-numbers mime python type

filetype.py |PyPI| |Pyversions| |API|

Small and dependency free Python package to infer file type and MIME type checking the magic numbers signature of a file or buffer.

This is a Python port from filetype_ Go package.

Features

Installation

::

pip install filetype

API

See annotated API reference_.

Examples

Simple file type checking ^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: python

import filetype

def main():
    kind = filetype.guess('tests/fixtures/sample.jpg')
    if kind is None:
        print('Cannot guess file type!')
        return

    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime)

if __name__ == '__main__':
    main()

Supported types

Image ^^^^^

Video ^^^^^

Audio ^^^^^

Archive ^^^^^^^

Document ^^^^^^^^

Font ^^^^

Application ^^^^^^^^^^^

.. _Python: http://python.org .. _magic numbers: https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files .. _filetype: https://github.com/h2non/filetype .. _wide range: #supported-types .. _Pluggable: #add-additional-file-type-matchers .. _Fast: #benchmarks .. _pass a list of bytes: #file-header .. _annotated API reference: https://h2non.github.io/filetype.py/

.. |PyPI| image:: https://img.shields.io/pypi/v/filetype.svg?maxAge=2592000?style=flat-square :target: https://pypi.python.org/pypi/filetype .. |Pyversions| image:: https://img.shields.io/pypi/pyversions/filetype.svg?style=flat-square :target: https://pypi.python.org/pypi/filetype .. |API| image:: https://img.shields.io/badge/api-docs-green.svg :target: https://h2non.github.io/filetype.py