Python Magic (Binary Bundled)

0.4.14 · active · verified Thu Apr 16

python-magic-bin is a Python wrapper for `libmagic`, the file type identification library. Unlike the original `python-magic` package, `python-magic-bin` bundles the `libmagic` binary and its associated magic database, making it easier to install and use, especially on platforms like Windows where `libmagic` is not natively available. It currently stands at version 0.4.14 and has a slow release cadence, primarily for bug fixes or compatibility updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `python-magic-bin` to identify the MIME type of a local file. The `magic.Magic()` constructor is used, typically with `mime=True` to get standard MIME types.

import magic
import os

# Create a dummy file for demonstration
with open('test_file.txt', 'w') as f:
    f.write('This is a test file.')

# Instantiate Magic to get MIME types
m = magic.Magic(mime=True)

# Identify a local file
file_path = 'test_file.txt'
file_type = m.from_file(file_path)
print(f"File '{file_path}' is of type: {file_type}")

# Clean up the dummy file
os.remove(file_path)

view raw JSON →