file-magic
raw JSON → 0.4.1 verified Mon Apr 27 auth: no python deprecated
Python front end for libmagic(3) — uses ctypes to wrap libmagic for file type detection. Current version 0.4.1, last released 2016; requires libmagic (system library). Unmaintained; prefer `python-magic` for active development.
pip install file-magic Common errors
error ImportError: No module named magic ↓
cause Both `file-magic` and `python-magic` create a module named `magic`. If both are installed, import may fail or wrong version used.
fix
Uninstall both and reinstall only one:
pip uninstall file-magic python-magic python-magic-bin then pip install python-magic. error OSError: libmagic not found ↓
cause System library libmagic is missing.
fix
Install libmagic: on Linux
sudo apt-get install libmagic1 (or equivalent), on macOS brew install libmagic. error TypeError: a bytes-like object is required, not 'str' ↓
cause In Python 3, file path must be bytes or string; often happens when passing string to .file() where lib expects bytes.
fix
Encode path if needed:
m.file(bytes(path, 'utf-8')) or ensure string path works (usual). Warnings
gotcha `file-magic` is unmaintained (last release 2016). Prefer `pip install python-magic` instead, which has a similar API and is actively maintained. ↓
fix Use `python-magic` for new projects.
gotcha Requires system libmagic installed. If missing, get an OSError or ImportError. ↓
fix Install libmagic (e.g., apt-get install libmagic1 on Debian/Ubuntu).
gotcha On Windows, libmagic is not preinstalled; need to bundle DLL or use alternative. ↓
fix Consider using `python-magic-bin` or `python-magic` with appropriate binary.
Imports
- magic
import magic
Quickstart
import magic
m = magic.open(magic.MAGIC_NONE)
m.load()
type_ = m.file('/path/to/file') # e.g., 'ASCII text'
print(type_)