Skip to main content

File type identification using libmagic

Project description

python-magic

PyPI version Build Status

python-magic 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 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 python-magic is available on pypi and can be installed by running pip install python-magic.

Other sources:

Windows

You'll need DLLs for libmagic. @julian-r has uploaded a version of this project that includes binaries to pypi: https://pypi-hypernode.com/pypi/python-magic-bin/0.4.14

Other sources of the libraries in the past have been File for Windows . You will need to copy the file magic out of [binary-zip]\share\misc, and pass its location to Magic(magic_file=...).

If you are using a 64-bit build of python, you'll need 64-bit libmagic binaries which can be found here: https://github.com/pidydx/libmagicwin64. Newer version can be found here: https://github.com/nscaife/file-windows.

OSX

  • When using Homebrew: brew install libmagic
  • When using macports: port install file

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

python-magic is distributed under the MIT license. See the included LICENSE file for details.

I am providing code in the repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not my employer (Facebook)

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.16-cp37-cp37m-manylinux1_x86_64.whl (232.1 kB view details)

Uploaded CPython 3.7m

magicfile-0.4.16-cp36-cp36m-manylinux1_x86_64.whl (232.1 kB view details)

Uploaded CPython 3.6m

magicfile-0.4.16-cp36-cp36m-macosx_10_6_x86_64.whl (10.1 kB view details)

Uploaded CPython 3.6m macOS 10.6+ x86-64

magicfile-0.4.16-cp35-cp35m-manylinux1_x86_64.whl (232.1 kB view details)

Uploaded CPython 3.5m

magicfile-0.4.16-cp34-cp34m-manylinux1_x86_64.whl (232.1 kB view details)

Uploaded CPython 3.4m

magicfile-0.4.16-cp27-cp27m-manylinux1_x86_64.whl (236.6 kB view details)

Uploaded CPython 2.7m

File details

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

File metadata

  • Download URL: magicfile-0.4.16-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 232.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.16-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 59da5cfd75c43d46d99c8897cfbf2cc6e1687da719b2901b9b06e38710c5adaa
MD5 cefab0f257675516fef9149a1b33df36
BLAKE2b-256 c1b9f2ffcb7021c2a562d0212488df6275fadd699a7a58e6c9a3bd479d8491eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magicfile-0.4.16-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 232.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.16-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c39ad046bb3a0b8dd42df2448b383acbdf8b280569f56ccf4d634ba77b299fce
MD5 0510133339d0151e86c9fdc2e614d744
BLAKE2b-256 ad72038c488c53d6b77ba8e40745b463f23f4956994288b69f3be9d1c207acf6

See more details on using hashes here.

File details

Details for the file magicfile-0.4.16-cp36-cp36m-macosx_10_6_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.16-cp36-cp36m-macosx_10_6_x86_64.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: CPython 3.6m, macOS 10.6+ 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.16-cp36-cp36m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 864bca107288f6b3f7b92c8fac175dbbc8405fd848a9fdedf872a26f03fce01a
MD5 74b682f7c14670654360fefbc74964be
BLAKE2b-256 86b76b3d571fa6039bff3619aaae43e157dcc311be51650b582261a7394af8ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magicfile-0.4.16-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 232.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.16-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 de08a77f27c04cc1a03414588f4ef489f75a607e2652f7065f5507a4b7965003
MD5 90be57762297fa7d37e8cc59dffe43d8
BLAKE2b-256 db39cfbac988bf1cc1edc62c7d2a75f05c3bc593b3ce73c173714d8c10509553

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magicfile-0.4.16-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 232.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.16-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e2336fdcdf303b79454145a1ec9b21d9771400d30e94cd2881747e28f65e529f
MD5 69af2715242e5337067a87047b2e41a8
BLAKE2b-256 f6e361897a41b502a43a5b91f1dd6b13538296b8a6aec26a944ee1ab5740ea3f

See more details on using hashes here.

File details

Details for the file magicfile-0.4.16-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: magicfile-0.4.16-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 236.6 kB
  • Tags: CPython 2.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.16-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4c6cc536bd222c9c8b92f0d245619095ba8dc7563ae8b4ca6499ab17db92dd8a
MD5 b93181c9365e33db23184a974e84eef6
BLAKE2b-256 889db800d83b7f9460d510f0554518278c6097a661d5b2e90ffc29b1752c801d

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