Skip to main content

File type identification using libmagic

Project description

magicfile (A CFFI fork of python-magic)

PyPI version

magicfile is a python interface to the libmagic file type identification library. libmagic identifies file types by checking their headers according to a predefined list of file types. This functionality is exposed to the command line by the Unix command file.

Usage

>>> import magicfile as magic
>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'
>>> magic.from_buffer(open("testdata/test.pdf").read(1024))
'PDF document, version 1.2'
>>> magic.from_file("testdata/test.pdf", 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, it's not safe for sharing across multiple threads and will fail throw if this is attempted.

>>> f = magic.Magic(uncompress=True)
>>> f.from_file('testdata/test.gz')
'ASCII text (gzip compressed data, was "test", last modified: Sat Jun 28
21:32:52 2008, from Unix)'

You can also combine the flag options:

>>> f = magic.Magic(mime=True, uncompress=True)
>>> f.from_file('testdata/test.gz')
'text/plain'

Versioning

Minor version bumps should be backwards compatible. Major bumps are not.

Name Conflict

There are, sadly, two libraries which use the module name magic. Both have been around for quite a while.If you are using this module and get an error using a method like open, your code is expecting the other one. Hopefully one day these will be reconciled.

Installation

The current stable version of magicfile is available on pypi and can be installed by running pip install magicfile.

Other sources:

Troubleshooting

  • 'MagicException: could not find any magic files!': some installations of libmagic do not correctly point to their magic database file. Try specifying the path to the file explicitly in the constructor: magic.Magic(magic_file="path_to_magic_file").

  • 'WindowsError: [Error 193] %1 is not a valid Win32 application': Attempting to run the 32-bit libmagic DLL in a 64-bit build of python will fail with this error. Here are 64-bit builds of libmagic for windows: https://github.com/pidydx/libmagicwin64

  • 'WindowsError: exception: access violation writing 0x00000000 ' This may indicate you are mixing Windows Python and Cygwin Python. Make sure your libmagic and python builds are consistent.

Author

Written by Adam Hupp in 2001 for a project that never got off the ground. It originally used SWIG for the C library bindings, but switched to ctypes once that was part of the python standard library.

You can contact me via my website or github.

Contributors

Thanks to these folks on github who submitted features and bugfixes.

License

magicfile is distributed under the MIT license. See the included LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magicfile-0.4.21.tar.gz (256.0 kB view details)

Uploaded Source

Built Distributions

magicfile-0.4.21-cp38-cp38-manylinux1_x86_64.whl (503.4 kB view details)

Uploaded CPython 3.8

magicfile-0.4.21-cp38-cp38-macosx_10_15_x86_64.whl (341.5 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

magicfile-0.4.21-cp37-cp37m-manylinux1_x86_64.whl (503.1 kB view details)

Uploaded CPython 3.7m

magicfile-0.4.21-cp37-cp37m-macosx_10_11_x86_64.whl (340.6 kB view details)

Uploaded CPython 3.7m macOS 10.11+ x86-64

magicfile-0.4.21-cp36-cp36m-manylinux1_x86_64.whl (503.1 kB view details)

Uploaded CPython 3.6m

magicfile-0.4.21-cp36-cp36m-macosx_10_11_x86_64.whl (340.6 kB view details)

Uploaded CPython 3.6m macOS 10.11+ x86-64

magicfile-0.4.21-cp35-cp35m-manylinux1_x86_64.whl (503.1 kB view details)

Uploaded CPython 3.5m

magicfile-0.4.21-cp34-cp34m-manylinux1_x86_64.whl (503.1 kB view details)

Uploaded CPython 3.4m

File details

Details for the file magicfile-0.4.21.tar.gz.

File metadata

  • Download URL: magicfile-0.4.21.tar.gz
  • Upload date:
  • Size: 256.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21.tar.gz
Algorithm Hash digest
SHA256 fc8d4d0a7d6a3d168bbb0f900d4dfdf105c821d852c3766dcb23bb3043a09351
MD5 6425098edf672fcfc0489b4e6a6a22ae
BLAKE2b-256 f7e3228b6af6f49f09c8b16be78b35c17fca5a906daaf1735fd3c30aff3e0fca

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 503.4 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for magicfile-0.4.21-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ba3abe7a2760af4749484343becd127a852c7ed57ccd37a6d16032ab151d6e6a
MD5 90640aabf16c17eb78118e2de326ee88
BLAKE2b-256 5c3db711685cf8fa638d13d0daa41f71e761fa70f480bc8c984837129caba318

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 341.5 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for magicfile-0.4.21-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 fe8a36f520a308b548d0cabd841315f7cd5bf044297c539cbc4df68406998ddf
MD5 fc55172f4c27f8e81f6f2b8f09454264
BLAKE2b-256 1dbaad49c289d570512221ea9a77b14394e3ebae0f5ced9acb3ba0d7e6d8df3d

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 503.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2d7310a061d111c1b618238db787fc217d1fa1df3b11c297c272a4f78531c666
MD5 b2e4ac04aca85ec567de40512bc6a17a
BLAKE2b-256 290efb24382cc329bdc8e629e42a97a3f1f52b082a9412ffca3b7e168323119c

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp37-cp37m-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp37-cp37m-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 340.6 kB
  • Tags: CPython 3.7m, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21-cp37-cp37m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 b09b7bc6edd3187782b4ad2c7a624ddad889e6940db04b4a620160b1a3468cbd
MD5 8c1965e6bdd4007aa7b8fa0cd48841c6
BLAKE2b-256 b75c77a6138b90ddb4be56360f41e6db54c8fa9a822254f3cf846918ce406bd6

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 503.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a8ee08e2b52e06373a68d23897efa83d285819361da18a0f68fdb95ec989399c
MD5 80a6975d1d7316d2dfd90a82e619a327
BLAKE2b-256 19d0cf32f5353eef81513f20ec69fc927f40847af5852881aae5c5210a2f5efa

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp36-cp36m-macosx_10_11_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp36-cp36m-macosx_10_11_x86_64.whl
  • Upload date:
  • Size: 340.6 kB
  • Tags: CPython 3.6m, macOS 10.11+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 93507ee9994541aee086d466a03e6c7b375cbe3d755f1bbffaa7b395b009b181
MD5 86f0437ca79a03dfe583915a3c131066
BLAKE2b-256 09a778b18cf3ec1a038981bbe3e9d3a9695d3ea824ec51b878de9e7a5b548b71

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 503.1 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 743e917d9f9f7dc3a0c8baab6961e34b31f570cd4ecfc356776d7be47b6901a0
MD5 5d0925676e2e5c406ebc34f16904cedc
BLAKE2b-256 aace6e8c26215efcfe4bde4e5dda62ee5240db2b7170a46b639804d118173289

See more details on using hashes here.

File details

Details for the file magicfile-0.4.21-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.21-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 503.1 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.2 CPython/2.7.10

File hashes

Hashes for magicfile-0.4.21-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0b6d3fa29daa60c49002b41f3c235ab480189bef2f975f7944b11dacbd32f492
MD5 759b8fe2d587b10a23930c97af3439e2
BLAKE2b-256 28ba9eea182fd51699d8f8d6efc3c89e40678ef2bd52bb1a4dbeee7c465d1f60

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page