A Python library for detecting file types by magic bytes
Project description
magicprobe
A zero-dependency Python library that detects file types by inspecting magic bytes — the binary signatures at the start of every file.
import magicprobe
result = magicprobe.probe("photo.jpg")
print(result.name) # JPEG
print(result.mime_type) # image/jpeg
print(result.extension) # .jpg
Features
- Zero dependencies — pure Python 3.10+
- Fast — reads only the first 262 bytes of each file
- Extensible — adding a new file type takes ~10 lines; see CONTRIBUTING.md
- Type-safe — fully typed, ships with
py.typed - CLI included —
magicprobe <file>works out of the box
Installation
pip install magicprobe
Usage
Detect a single file type
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) # "ZIP"
print(result.mime_type) # "application/zip"
print(result.extensions) # (".zip",)
print(result.extension) # ".zip" ← primary extension shortcut
else:
print("Unknown file type")
Detect all matching types
Some formats are containers (e.g. a DOCX file is also a ZIP file).
probe_all() returns every matching type, ordered from most specific to least specific:
results = magicprobe.probe_all("document.docx")
for r in results:
print(r.name) # OOXML, then ZIP
Command-line interface
# One file
magicprobe image.png
# image.png: PNG (image/png) [.png]
# Multiple files
magicprobe *
Supported types
| Category | Types |
|---|---|
| Image | PNG, JPEG, GIF, WEBP, BMP, TIFF, ICO, AVIF, HEIC |
| Document | PDF, RTF, OLE2 (doc/xls/ppt), OOXML (docx/xlsx/pptx), EPUB |
| Archive | ZIP, GZIP, BZIP2, XZ, 7-Zip, RAR, TAR, Zstandard, LZ4 |
| Audio | MP3, WAV, FLAC, OGG, OPUS, M4A, AIFF |
| Video | MP4, MOV, AVI, WEBM, MKV, FLV, WMV/ASF |
| Executable | ELF, PE (exe/dll), Mach-O, DEX (Android), WASM |
Want to add a new type?
See CONTRIBUTING.md — it takes about 10 lines of code and a test.
API reference
magicprobe.probe(source) → ProbeResult | None
Detect the file type of source (file path or bytes).
Returns the first matching ProbeResult, or None if unknown.
magicprobe.probe_all(source) → list[ProbeResult]
Returns all matching ProbeResult objects (useful for container formats).
ProbeResult
| Attribute | Type | Description |
|---|---|---|
name |
str |
Human-readable name, e.g. "PNG" |
mime_type |
str |
IANA MIME type, e.g. "image/png" |
extensions |
tuple[str, …] |
File extensions, e.g. (".png",) |
extension |
str | None |
Primary extension (property) |
Project structure
src/magicprobe/
├── __init__.py ← public API
├── probe.py ← probe() / probe_all()
├── result.py ← ProbeResult dataclass
├── registry.py ← @register decorator & get_all()
├── __main__.py ← CLI entry point
└── types/
├── base.py ← FileType ABC
├── image.py
├── document.py
├── archive.py
├── audio.py
├── video.py
└── executable.py
Contributing
Contributions are welcome!
Please read CONTRIBUTING.md before opening a PR.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file magicprobe_core-0.1.0.tar.gz.
File metadata
- Download URL: magicprobe_core-0.1.0.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3806745dafa4ae1d036c6b653dec8e62d23abd9619f3510421f962561ed448a
|
|
| MD5 |
c2cd5ea593fbb78b1d6538acca436133
|
|
| BLAKE2b-256 |
4afaef692a40f5c5560b31f144fc6ff65015850e3775c8709dc98bb54986af33
|
File details
Details for the file magicprobe_core-0.1.0-py3-none-any.whl.
File metadata
- Download URL: magicprobe_core-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5644c058692569bafffcc016c9273d86824851c4b2f43946e09f2332cd48811e
|
|
| MD5 |
d8b93c739f4c957529b7433e80b27679
|
|
| BLAKE2b-256 |
d6f4670f9f05c111606a5c7a505b3ece3dd11436fc3676ce55d30af6affa9ec0
|