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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

magicfile-0.4.27-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (616.2 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

magicfile-0.4.27-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (616.3 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

magicfile-0.4.27-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (616.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

magicfile-0.4.27-cp38-cp38-macosx_12_0_arm64.whl (474.9 kB view details)

Uploaded CPython 3.8 macOS 12.0+ ARM64

magicfile-0.4.27-cp38-cp38-macosx_10_14_x86_64.whl (479.1 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

magicfile-0.4.27-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (616.2 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

magicfile-0.4.27-cp37-cp37m-macosx_10_14_x86_64.whl (479.1 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

magicfile-0.4.27-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (616.2 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

magicfile-0.4.27-cp36-cp36m-macosx_10_14_x86_64.whl (479.1 kB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

File details

Details for the file magicfile-0.4.27-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for magicfile-0.4.27-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 dbd07816497dbb769b81a2dc0bb9a5addec13c803d90e34d1bb00d801d60dd95
MD5 b1089b33b921a40e3dd7db624c02cdc9
BLAKE2b-256 7c7051950831fdc2a73471c1087243b4e4c99f1c6e59e9c42a52cb78a64817c0

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for magicfile-0.4.27-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 32a4dda8f947ede2d0f228671e7d5c21339f8ef1998d25d82d0640145108810f
MD5 6b676ad2580187a197fbefc6cb40e86f
BLAKE2b-256 380b6d1509be3a8eb5d4d7af5db0ed63a0721aac4cd780c78900b57ecf9cafef

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for magicfile-0.4.27-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 2bf4fabb2a166648cd2704a312e5573e4f50ade54bbb2c052894795c23171dcf
MD5 742a84c7c98f4b71ebde3a3ab66879b8
BLAKE2b-256 9c0baddb1b137cb65cae0596bf62ab09638962079b6ef31a1802f4a7915a4b07

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp38-cp38-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for magicfile-0.4.27-cp38-cp38-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 ef66e7ee687988ee830a95259c5a62cae48ff39694c421182de0e7e6b69eec4c
MD5 62a81f0f59ac8c582f80a3af4af66512
BLAKE2b-256 4bf6a77034c051c6f689185dc42051377216bb72ceeeb137544807f2af7d6c46

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.27-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 479.1 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for magicfile-0.4.27-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 99d94205ce66631f1fe1117b56b3a3734f4be20e3938fc5d2694e21d9d40c166
MD5 1489f3a3bc44672aa0b853548e07780b
BLAKE2b-256 eecef07c313bf80b747238b9c3b61b68210ba7a7f2ac2fc4f48466c91ce1cd2f

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for magicfile-0.4.27-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b7531c947cb487ccd80a116c5427e762744b6daf062454294e4c9224b44691cd
MD5 60cfb45dd9138750849c92a9530eab71
BLAKE2b-256 8e3a732f7518aa8d29635580635965391e549b60b5db34325d60732d026343bf

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.27-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 479.1 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for magicfile-0.4.27-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 3d55827f0f51a6fb25384b7066ad7588609155c85816f3d4bbeca2349b328c00
MD5 d357ea44dbc5987c7a13d1a53885d293
BLAKE2b-256 09b37e09b70723f35fbabd2ba83fff97483780af146e97b60139a3b13bb5d9f2

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for magicfile-0.4.27-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 578d577c5730aa000edbd03691f0f3d31a679cd2d1bf6faa87e472baec8a7e08
MD5 eadef49885692518168a27b4cd6bd511
BLAKE2b-256 1b788469be2ceeac967cfbb34edef8bb5c6149238982895f9b77ba60fce9b038

See more details on using hashes here.

File details

Details for the file magicfile-0.4.27-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.27-cp36-cp36m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 479.1 kB
  • Tags: CPython 3.6m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

File hashes

Hashes for magicfile-0.4.27-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c119b660baeb91e167c7cfd54cf3dedebcc14cfa8ed57fdcff074beef2388935
MD5 effa7eebfb1ba76e7908a8043840db64
BLAKE2b-256 d1268ef1792c1e46235c966d4b72c1f552533b976e96480e6b5e3053d11f06b5

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