Skip to main content

A pure Python magic file identifier

Project description

========= puremagic

puremagic is a pure python module that will identify a file based off it's magic numbers.

It is designed to be minimalistic and inherently cross platform compatible. It is also designed to be a stand in for python-magic, it incorporates the functions from_file(filename[, mime]) and from_string(string[, mime]) however the magic_file() and magic_string() are more powerful and will also display confidence and duplicate matches.

It does NOT try to match files off non-magic string. In other words it will not search for a string within a certain window of bytes like others might.

Advantages over using a wrapper for 'file' or 'libmagic':

  • Faster
  • Lightweight
  • Cross platform compatible
  • No dependencies

Disadvantages:

  • Does not have as many file types
  • No multilingual comments
  • Duplications due to small or reused magic numbers

(Help fix the first two disadvantages by contributing!)

Usage

"from_file" will return the most likely file extension. "magic_file" will give you every possible result it finds, as well as the confidence.

.. code:: python

    import puremagic

    filename = "test/resources/images/test.gif"

    ext = puremagic.from_file(filename)
    # '.gif'

    puremagic.magic_file(filename)
    # [['.gif', 'image/gif', 'Graphics interchange format file (GIF87a)', 0.7],
    #  ['.gif', '', 'GIF file', 0.5]]

With "magic_file" it gives each match, highest confidence first:

  • possible extension(s)
  • mime type
  • description
  • confidence (All headers have to perfectly match to make the list, however this orders it by longest header, therefore most precise, first)

If you already have a file open, or raw byte string, you could also use:

  • from_string
  • from_stream
  • magic_string
  • magic_stream

.. code:: python

    with open(r"test\resources\video\test.mp4", "rb") as file:
        print(puremagic.magic_stream(file))

    # [PureMagicWithConfidence(byte_match=b'ftypisom', offset=4, extension='.mp4', mime_type='video/mp4', name='MPEG-4 video', confidence=0.8),
    #  PureMagicWithConfidence(byte_match=b'iso2avc1mp4', offset=20, extension='.mp4', mime_type='video/mp4', name='MP4 Video', confidence=0.8)]

Script

Usage

.. code:: bash

    $ python -m puremagic [options] filename <filename2>...

Examples

.. code:: bash

    $ python -m puremagic test/resources/images/test.gif
    'test/resources/images/test.gif' : .gif

    $ python -m puremagic -m test/resources/images/test.gif test/resources/audio/test.mp3
    'test/resources/images/test.gif' : image/gif
    'test/resources/audio/test.mp3' : audio/mpeg

imghdr replacement

If you are looking for a replacement for the standard library's depreciated imghdr, you can use puremagic.what()

.. code:: python

    import puremagic

    filename = "test/resources/images/test.gif"

    ext = puremagic.what(filename)
    # 'gif'

FAQ

The file type is actually X but it's showing up as Y with higher confidence?

This can happen when the file's signature happens to match a subset of a file standard. The subset signature will be longer, therefore report with greater confidence, because it will have both the base file type signature plus the additional subset one.

You don't have sliding offsets that could better detect plenty of common formats, why's that?

Design choice, so it will be a lot faster and more accurate. Without more intelligent or deeper identification past a sliding offset I don't feel comfortable including it as part of a 'magic number' library.

Your version isn't as complete as I want it to be, where else should I look?

Look into python modules that wrap around libmagic or use something like Apache Tika.

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

pmagic-1.4.7.tar.gz (44.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pmagic-1.4.7-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file pmagic-1.4.7.tar.gz.

File metadata

  • Download URL: pmagic-1.4.7.tar.gz
  • Upload date:
  • Size: 44.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pmagic-1.4.7.tar.gz
Algorithm Hash digest
SHA256 ffe924d936d0ba9b5349da645dd320fb08008ea3879daa7341a0cb9d2f298d22
MD5 08f5969db65f3ffd983d399fa9985b0e
BLAKE2b-256 656d217f6871f873410eb4e413e36a72e7e2fc0a555e164a3cb28428b06c3b17

See more details on using hashes here.

File details

Details for the file pmagic-1.4.7-py3-none-any.whl.

File metadata

  • Download URL: pmagic-1.4.7-py3-none-any.whl
  • Upload date:
  • Size: 43.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pmagic-1.4.7-py3-none-any.whl
Algorithm Hash digest
SHA256 48f861f5ece6d7c1b87df5fb8a6d208e45da54e90d63d9bf236b7d083b4d1097
MD5 79891e7f5a823eb9a92ff36915039b9c
BLAKE2b-256 78dbf2cf688f657dc3d3bfdcf4cdb5f179ff93a2b684d42912365b41c5e8f700

See more details on using hashes here.

Supported by

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