file-magic
Python front end for libmagic(3)
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires libmagic system library to be installed on the host system (typically available via system package manager).
- Low install friction; pure Python wheel with no runtime dependencies.
- Repository is active with recent commits and moderate GitHub engagement (1647 stars).
License · maintenance · safety
permissive license (permissive) — Permissive BSD-style license (copyright Ian F. Darwin and contributors) allows commercial and private use with minimal restrictions—attribution and disclaimer retention required.
last release 2022-09-01 (1443 days) · last repo commit 2026-08-05 · 1,647 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 84,589 downloads/mo, #13,984 on PyPI
Alternatives
Verify before relying
import magic
detected = magic.detect_from_filename('example.txt')
print(detected.mime_type)
print(detected.encoding)
print(detected.name)- 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.
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 on it.
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
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).
Requires libmagic system library to be installed on the host system (typically available via system package manager).
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)
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 | permissive license 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 |
| Classifiers | License :: OSI Approved :: BSD LicenseProgramming Language :: PythonProgramming Language :: Python :: 3 |
Evidence: file_magic-0.4.1-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “file type detection python”
- file-magicfile-magic provides a Python interface to libmagic, enabling…
- magikaMagika identifies file types using deep learning, analyzing file…
- libmagicProvides Python bindings to the libmagic library for file type…
Give your agent the search over MCP, or paste the wish link into any chat.
More Utilities packages
Converts domain names between Unicode and ASCII-compatible encoding (Punycode) according to IDNA 2008 and Unicode Technical Standard 46, with security validation and broader script coverage than the standard library.
Install it if you work with internationalized domain names, need to validate domains, or use HTTP clients that depend on it transitively.
Detects and normalizes text encoding from unknown or ambiguous sources, supporting all IANA character sets that Python's core library provides codecs for, with the ability to register custom codecs.
Setuptools is a Python build backend and package management tool that handles building, distributing, and installing Python packages, including support for C/C++ extension modules.
Pluggy provides a plugin system that lets you define hook specifications and register implementations to be called in sequence, enabling extensible Python applications without tight coupling.
Install it if you're building an extensible application or framework.
Pygments is a syntax highlighter that colorizes source code and text in over 500 languages and formats, outputting to HTML, LaTeX, RTF, SVG, images, or ANSI terminal sequences.
Install it if you need to display or transform source code.
Six provides utility functions to write Python code that runs on both Python 2.7 and Python 3.3+, smoothing over language differences between the two versions.
See also libmagic · python-magic · python-magic-bin · pylibmagic · cigam · filetype · puremagic · polyfile-weave · typecode · isbinary