Skip to main content

File type identification using libmagic

Project description

actrecipemagicfile (A CFFI fork of python-magic)

PyPI version

actrecipemagicfile is a Python interface to the libmagic file type identification library. libmagic identifies file types by checking their headers against a predefined list. This functionality is accessible from the command line using 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'

License

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

Note: This package is mostly used in an internal project.

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

actrecipemagicfile-0.4.31-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (637.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

actrecipemagicfile-0.4.31-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (637.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

actrecipemagicfile-0.4.31-cp311-cp311-macosx_10_9_universal2.whl (499.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

actrecipemagicfile-0.4.31-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (637.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

actrecipemagicfile-0.4.31-cp310-cp310-macosx_11_0_x86_64.whl (494.0 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

actrecipemagicfile-0.4.31-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (637.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

actrecipemagicfile-0.4.31-cp39-cp39-macosx_11_0_x86_64.whl (494.0 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

actrecipemagicfile-0.4.31-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (637.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

actrecipemagicfile-0.4.31-cp38-cp38-macosx_11_0_x86_64.whl (494.0 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

actrecipemagicfile-0.4.31-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (636.9 kB view hashes)

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

actrecipemagicfile-0.4.31-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (636.9 kB view hashes)

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

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