Skip to main content

A Python library for detecting file types by magic bytes

Project description

magicprobe

A Python library that detects file types by delegating to the system libmagic C library via ctypes. No third-party Python packages are required.

import magicprobe

result = magicprobe.probe("photo.jpg")
if result:
    print(result.mime_type)  # image/jpeg

Requirements

  • Python 3.10+
  • The libmagic shared library installed on the system:
# macOS
brew install libmagic

# Debian / Ubuntu
apt install libmagic1

Installation

pip install magicprobe

Usage

Detect the type of a file

import magicprobe

# From a file path (str or pathlib.Path)
result = magicprobe.probe("archive.zip")

# From raw bytes
with open("file.bin", "rb") as f:
    result = magicprobe.probe(f.read())

if result:
    print(result.name)       # "application/zip"
    print(result.mime_type)  # "application/zip"
else:
    print("Unknown file type")

Detect all matching types

probe_all() returns every MIME type libmagic reports for the source, ordered from most to least specific:

results = magicprobe.probe_all("document.docx")
for r in results:
    print(r.mime_type)

Command-line interface

magicprobe image.png
# image.png: image/png (image/png)  [—]

magicprobe file1 file2 file3

API reference

magicprobe.probe(source) → ProbeResult | None

Detect the file type of source.

  • source — a file path (str or pathlib.Path) or raw bytes / bytearray.
  • Returns the first matching ProbeResult, or None if libmagic cannot identify the type.

magicprobe.probe_all(source) → list[ProbeResult]

Same as probe() but returns all matching ProbeResult objects. Returns an empty list if the type is unknown.

ProbeResult

Attribute Type Description
name str MIME type string returned by libmagic, e.g. "image/png"
mime_type str IANA MIME type, e.g. "image/png"
extensions tuple[str, ...] File extensions (empty tuple in the current release)
extension str | None Primary extension, or None (property)

Project structure

src/magicprobe/
├── __init__.py      — public API (probe, probe_all, ProbeResult)
├── probe.py         — probe() / probe_all() implementation
├── result.py        — ProbeResult dataclass
├── libmagic_c.py    — ctypes wrapper for the system libmagic library
└── __main__.py      — CLI entry point

Contributing

Contributions are welcome.
See CONTRIBUTING.md for setup instructions and guidelines.

License

MIT

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

magicprobe_core-0.2.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

magicprobe_core-0.2.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file magicprobe_core-0.2.0.tar.gz.

File metadata

  • Download URL: magicprobe_core-0.2.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for magicprobe_core-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c0286a1fb2732f7190fea01b2d33fc9bf513a6943c84404b71de5716f1f3788b
MD5 1a5ef130251ae0a94d7ac16b5cdecd96
BLAKE2b-256 351436582d1ceb531e13269cb2acc21a7661a377307e512b5d19465b2b7964c8

See more details on using hashes here.

File details

Details for the file magicprobe_core-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for magicprobe_core-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e7a2cae2fdab2f77a4475527330e886434bf87f6a338bfa7083e9ac7f6d19c8
MD5 0b52eb3fea28fa33e540e0eb9800a62f
BLAKE2b-256 643473854490860c53a5bf2241c932adb0ce771fe8d656ab327eb8043765c50d

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