Package: 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’:FasterLightweightCross platform compatibleNo dependenciesDisadvantages:Does not have as many file typesNo multilingual commentsDuplications due to small or reused magic numbers(Help fix the first two disadvantages by contributing!)CompatibilityPython 3.8+Using github ci to run continuous integration tests on listed platforms.Install from pypy$pipinstallpuremagicOn linux environments, you may want to be clear you are using python3$python3-mpipinstallpuremagicInstall from sourceIn either a virtualenv or globally, simply run:$pythonsetup.pyinstallUsage“from_file” will return the most likely file extension. “magic_file”
will give you every possible result it finds, as well as the confidence.importpuremagicfilename="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 typedescriptionconfidence (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_stringfrom_streammagic_stringmagic_streamwithopen(r"test\resources\video\test.mp4","rb")asfile: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)]ScriptUsage$python-mpuremagic[options]filename<filename2>...Examples$python-mpuremagictest/resources/images/test.gif'test/resources/images/test.gif':.gif$python-mpuremagic-mtest/resources/images/test.giftest/resources/audio/test.mp3'test/resources/images/test.gif':image/gif'test/resources/audio/test.mp3':audio/mpegFAQThe 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.AcknowledgementsGary C. KesslerFor use of his File Signature Tables, available at:http://www.garykessler.net/library/file_sigs.htmlFreedesktop.orgFor use of their shared-mime-info file (even if they do use XML, blea), available at:https://cgit.freedesktop.org/xdg/shared-mime-info/LicenseMIT Licenced, see LICENSE, Copyright (c) 2013-2024 Chris Griffith