skillfed

python-magic

File type identification using libmagic

python-magic Permissive license MIT Active 2,914 v0.4.27 released

Install

python-magic on PyPI

pip

pip install python-magic

uv

uv add python-magic

poetry

poetry add python-magic

Package facts

License MIT (permissive)
Python support supports the current Python release (>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 1,528 days since the last release
Last repo commit
First released
Popularity one of the top 1,000 most-downloaded packages on PyPI (30-day window, as of 2026-08-13)
Known vulnerabilities none known (OSV.dev, checked 2026-08-13)

Evidence: python_magic-0.4.27-py2.py3-none-any.whl

Keywords: mime, magic, file

Intended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: PythonProgramming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPython

About python-magic

from the package's own PyPI description — quoted content, verbatim

python-magic

PyPI version (image) Build Status (image) Join the chat at https://gitter.im/ahupp/python-magic (image)

python-magic is a Python interface to the libmagic file type identification library. libmagic identifies file types by checking their headers according to a predefined list of file types. This functionality is exposed to the command line by the Unix command file.

Usage

>>> import magic
>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'
# recommend using at least the first 2048 bytes, as less can produce incorrect identification
>>> magic.from_buffer(open("testdata/test.pdf", "rb").read(2048))
'PDF document, version 1.2'
>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'

There is also a Magic class that provides more direct control, including overriding the magic...

Read as markdown · JSON record · Source repository · Homepage

Similar packages