Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
PyPI

puremagic 1.30

pip install puremagic

Latest version

Released:

Pure python implementation of magic file detection

Verified details

These details have beenverified by PyPI
Maintainers
Avatar for cdgriffith from gravatar.comcdgriffith

Unverified details

These details havenot been verified by PyPI
Project links
Meta
Classifiers

Project description

puremagic is a pure python module that will identify a file based offit’s magic numbers.

CoverageStatusLicensePyPi

It is designed to be minimalistic and inherently cross platformcompatible. It is also designed to be a stand in for python-magic, itincorporates the functions from_file(filename[, mime]) andfrom_string(string[, mime]) however the magic_file() andmagic_string() are more powerful and will also display confidence andduplicate matches.

It does NOT try to match files off non-magic string. In other words itwill not search for a string within a certain window of bytes likeothers 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!)

Compatibility

  • Python 3.7+

Using github ci to run continuous integration tests on listed platforms.

Install from pypy

$pipinstallpuremagic

On linux environments, you may want to be clear you are using python3

$python3-mpipinstallpuremagic

Install from source

In either a virtualenv or globally, simply run:

$pythonsetup.pyinstall

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.

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 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

withopen(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)]

Script

Usage

$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/mpeg

imghdr replacement

If you are looking for a replacement for the standard library’s depreciated imghdr, you can usepuremagic.what()

importpuremagicfilename="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 higherconfidence?

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

You don’t have sliding offsets that could better detect plenty ofcommon formats, why’s that?

Design choice, so it will be a lot faster and more accurate. Withoutmore intelligent or deeper identification past a sliding offset I don’tfeel 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 Ilook?

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

Acknowledgements

Gary C. Kessler

For use of his File Signature Tables, available at:http://www.garykessler.net/library/file_sigs.html

Freedesktop.org

For use of their shared-mime-info file, available at:https://cgit.freedesktop.org/xdg/shared-mime-info/

License

MIT Licenced, see LICENSE, Copyright (c) 2013-2025 Chris Griffith

Project details

Verified details

These details have beenverified by PyPI
Maintainers
Avatar for cdgriffith from gravatar.comcdgriffith

Unverified details

These details havenot been verified by PyPI
Project links
Meta
Classifiers

Download files

Download the file for your platform. If you're not sure which to choose, learn more aboutinstalling packages.

Source Distribution

puremagic-1.30.tar.gz (314.8 kBview details)

UploadedSource

Built Distribution

Filter files by name, interpreter, ABI, and platform.

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

Copy a direct link to the current filters

puremagic-1.30-py3-none-any.whl (43.3 kBview details)

UploadedPython 3

File details

Details for the filepuremagic-1.30.tar.gz.

File metadata

  • Download URL:puremagic-1.30.tar.gz
  • Upload date:
  • Size: 314.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for puremagic-1.30.tar.gz
AlgorithmHash digest
SHA256f9ff7ac157d54e9cf3bff1addfd97233548e75e685282d84ae11e7ffee1614c9
MD56dffa5de27c361c21492c74a7d97a119
BLAKE2b-256dd7f9998706bc516bdd664ccf929a1da6c6e5ee06e48f723ce45aae7cf3ff36e

See more details on using hashes here.

File details

Details for the filepuremagic-1.30-py3-none-any.whl.

File metadata

  • Download URL:puremagic-1.30-py3-none-any.whl
  • Upload date:
  • Size: 43.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for puremagic-1.30-py3-none-any.whl
AlgorithmHash digest
SHA2565eeeb2dd86f335b9cfe8e205346612197af3500c6872dffebf26929f56e9d3c1
MD5a25756c9d3ed2c0c560e3f02c208a417
BLAKE2b-25691ed1e347d85d05b37a8b9a039ca832e5747e1e5248d0bd66042783ef48b4a37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security SponsorDatadog MonitoringDepot Continuous IntegrationFastly CDNGoogle Download AnalyticsPingdom MonitoringSentry Error loggingStatusPage Status page

[8]ページ先頭

©2009-2026 Movatter.jp