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 (strorpathlib.Path) or rawbytes/bytearray.- Returns the first matching
ProbeResult, orNoneif 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
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.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0286a1fb2732f7190fea01b2d33fc9bf513a6943c84404b71de5716f1f3788b
|
|
| MD5 |
1a5ef130251ae0a94d7ac16b5cdecd96
|
|
| BLAKE2b-256 |
351436582d1ceb531e13269cb2acc21a7661a377307e512b5d19465b2b7964c8
|
File details
Details for the file magicprobe_core-0.2.0-py3-none-any.whl.
File metadata
- Download URL: magicprobe_core-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.6 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 |
9e7a2cae2fdab2f77a4475527330e886434bf87f6a338bfa7083e9ac7f6d19c8
|
|
| MD5 |
0b52eb3fea28fa33e540e0eb9800a62f
|
|
| BLAKE2b-256 |
643473854490860c53a5bf2241c932adb0ce771fe8d656ab327eb8043765c50d
|