file-magic
Python front end for libmagic(3)
What it is and what it does
file-magic is a ctypes wrapper around the libmagic library, which is the same engine used by the Unix `file` command. It lets you identify what a file actually is—its MIME type, character encoding, and human-readable type name—by examining its content or path, rather than relying on file extensions.
The package has no Python runtime dependencies and installs as a pure wheel, making it lightweight to add to any project. It works by calling into the native libmagic library, so you need libmagic available on your system (usually pre-installed on Linux/macOS, or installable via package manager). The API is straightforward: pass a filename to `detect_from_filename()` and get back an object with `mime_type`, `encoding`, and `name` attributes.
Use it for:
- Validate uploaded files in web applications by checking actual MIME type instead of trusting user-supplied extensions.
- Automatically categorize files in batch processing pipelines based on detected type and encoding.
- Build file management tools that need to identify file types for sorting, filtering, or routing decisions.
- Detect character encoding of text files to handle them correctly during import or conversion workflows.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
file-magic provides a Python interface to libmagic, enabling detection of file types, MIME types, and encodings from file paths or content.
Yes. file-magic is a stable, low-friction wrapper around a well-established system library. It has no Python dependencies, installs cleanly, carries a permissive license, and solves a real problem—reliable file type detection—that's otherwise tedious to implement. The main gotcha is the libmagic system dependency, which is typically already present but must be verified in your deployment environment.
Install
file-magic on PyPI
pip
pip install file-magicuv
uv add file-magicpoetry
poetry add file-magicInstalling file-magic
Before you install
Low install friction; pure Python wheel with no runtime dependencies. Repository is active with recent commits and moderate GitHub engagement (1647 stars).
License in practice
Permissive BSD-style license (copyright Ian F. Darwin and contributors) allows commercial and private use with minimal restrictions—attribution and disclaimer retention required.
Quickstart
import magic
detected = magic.detect_from_filename('example.txt')
print(detected.mime_type)
print(detected.encoding)
print(detected.name)
Requires libmagic system library to be installed on the host system (typically available via system package manager).
Verify before relying
- Whether libmagic is pre-installed on common deployment targets or must be added as a system dependency.
- Performance characteristics when analyzing large files or high-throughput scenarios.
Package facts
| License | Copyright (c) Ian F. Darwin 1986-1995. Software written by Ian F. Darwin and others; maintained 1995-present by Christos Zoulas and others. Redistribution and use in source and binary forms, with or… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=2.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 1,443 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 84,589/month — #13,984 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: file_magic-0.4.1-py3-none-any.whl
Keywords: file, magic
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
libmagicProvides Python bindings to the libmagic…
copyleft · top 15,000 on PyPI
python-magicIdentifies file types by reading file headers…
permissive · top 1,000 on PyPI
python-magic-binIdentifies file types by examining file headers…
permissive · top 15,000 on PyPI
pylibmagicProvides pre-compiled libmagic libraries…
permissive · top 15,000 on PyPI
cigamDetects and reports file types, MIME types, and…
permissive · top 15,000 on PyPI
filetypeInfers file type and MIME type by examining…
permissive · top 1,000 on PyPI
puremagicIdentifies file types by analyzing magic…
permissive · top 5,000 on PyPI
polyfile-weaveIdentifies and maps the semantic and syntactic…
permissive · top 5,000 on PyPI
typecodeDetects file types and MIME types using…
permissive · top 15,000 on PyPI
isbinaryDetermines whether a file is binary or text by…
permissive · top 15,000 on PyPI