File type identification using libmagic
Project description
magicfile (A CFFI fork of python-magic)
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.
- Amit Sethi
- bigben87
- fallgesetz
- FlaPer87
- lukenowak
- NicolasDelaby
- sacha@ssl.co.uk
- SimpleSeb
- tehmaze
License
magicfile is distributed under the MIT license. See the included LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file magicfile-0.4.26-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
.
File metadata
- Download URL: magicfile-0.4.26-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
- Upload date:
- Size: 616.2 kB
- Tags: CPython 3.10, manylinux: glibc 2.12+ 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.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de9e3b41ed4badb92dd9cc2d27dc80e817a741de5679e2856579be28f4e48865 |
|
MD5 | ef36a815abe784caaf25496de8eaec00 |
|
BLAKE2b-256 | f3062be794e3f2db134aaee20e56adbfeed663cc74bbade324b0001e8c4a5994 |
File details
Details for the file magicfile-0.4.26-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
.
File metadata
- Download URL: magicfile-0.4.26-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
- Upload date:
- Size: 616.3 kB
- Tags: CPython 3.9, manylinux: glibc 2.12+ 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.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 134133d3617d78e2fa2f72093cef8d07c2e8a37591f00025e09958a76f0bb354 |
|
MD5 | 5e96387282033797e6152ad3a35a85ca |
|
BLAKE2b-256 | f676097ac98088580d07b052adca81879c1dc18b7fa30ec4f1de047cc323f18c |
File details
Details for the file magicfile-0.4.26-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
.
File metadata
- Download URL: magicfile-0.4.26-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
- Upload date:
- Size: 616.7 kB
- Tags: CPython 3.8, manylinux: glibc 2.12+ 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.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 767116f20f7e9df0f879b208386802931c393ce0e4d55adfa847df3375c7ebf8 |
|
MD5 | 4662479539327bf7f25623ae0fef7569 |
|
BLAKE2b-256 | 2bb322e8427685c916ffd8444da3d876c5f0c67b38a74e3979b73e4907b49ede |
File details
Details for the file magicfile-0.4.26-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
.
File metadata
- Download URL: magicfile-0.4.26-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
- Upload date:
- Size: 616.2 kB
- Tags: CPython 3.7m, manylinux: glibc 2.12+ 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.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9605952c12015a2e5d5804dc5929e41c6f84dd6225b3fe75bb91aa2d92cb68d0 |
|
MD5 | c180691e881ed7b0b37cfb3e9dede5bf |
|
BLAKE2b-256 | c46fd4818ccc3743a8352f71069f927d2eec7176647e73d8e119aa63f9226f53 |
File details
Details for the file magicfile-0.4.26-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
.
File metadata
- Download URL: magicfile-0.4.26-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
- Upload date:
- Size: 616.2 kB
- Tags: CPython 3.6m, manylinux: glibc 2.12+ 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.9.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78c80426bc50908fe276fbc069a43950b39f8ce943af0596fc360711d3fc8732 |
|
MD5 | 0712ff66c25c894f2fdb4257349437d3 |
|
BLAKE2b-256 | 13b8fb4d74ab5213d910bce0a7bc6925670c8209ae494cc4aae27018f213ea36 |
File details
Details for the file magicfile-0.4.26-cp36-cp36m-macosx_10_14_x86_64.whl
.
File metadata
- Download URL: magicfile-0.4.26-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7808fa7cc485d3a9ed626c857999968f17c4c4286399ed1ce735ca957edd1fd3 |
|
MD5 | b706ff81b5fdf16bfb5fce77280776c3 |
|
BLAKE2b-256 | d9adc2bf3c266b047eb3216ba86093d67e2bddd48bca06eadbeb3f2ef736877e |