Infer file type and MIME type of any file/buffer. No external dependencies.
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.
wide range_ of file typesPluggable_: add new custom type matchersFast_, even processing large filespass a list of bytes_::
pip install filetype
See annotated API reference_.
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()
Image ^^^^^
image/vnd.dwgimage/x-xcfimage/jpegimage/jpximage/pngimage/apngimage/gifimage/webpimage/x-canon-cr2image/tiffimage/bmpimage/vnd.ms-photoimage/vnd.adobe.photoshopimage/x-iconimage/heicimage/avifVideo ^^^^^
video/3gppvideo/mp4video/x-m4vvideo/x-matroskavideo/webmvideo/quicktimevideo/x-msvideovideo/x-ms-wmvvideo/mpegvideo/x-flvAudio ^^^^^
audio/aacaudio/midiaudio/mpegaudio/mp4audio/oggaudio/x-flacaudio/x-wavaudio/amraudio/x-aiffArchive ^^^^^^^
application/x-brotliapplication/x-rpmapplication/dicomapplication/epub+zipapplication/zipapplication/x-tarapplication/x-rar-compressedapplication/gzipapplication/x-bzip2application/x-7z-compressedapplication/x-xzapplication/pdfapplication/x-msdownloadapplication/x-shockwave-flashapplication/rtfapplication/octet-streamapplication/postscriptapplication/x-sqlite3application/x-nintendo-nes-romapplication/x-google-chrome-extensionapplication/vnd.ms-cab-compressedapplication/x-debapplication/x-unix-archiveapplication/x-compressapplication/x-lzopapplication/x-lzipapplication/x-lz4application/zstdDocument ^^^^^^^^
application/mswordapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.oasis.opendocument.textapplication/vnd.ms-excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.oasis.opendocument.spreadsheetapplication/vnd.ms-powerpointapplication/vnd.openxmlformats-officedocument.presentationml.presentationapplication/vnd.oasis.opendocument.presentationFont ^^^^
application/font-woffapplication/font-woffapplication/font-sfntapplication/font-sfntApplication ^^^^^^^^^^^
application/wasm.. _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/
.. |Build Status| image:: https://travis-ci.org/h2non/filetype.py.svg?branch=master :target: https://travis-ci.org/h2non/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