python-magic
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 `file` command. The current version is 0.4.27, with releases occurring infrequently, often with several months or years between updates.
Warnings
- breaking The module name `magic` is also used by other (often older or less maintained) Python packages like `file-magic` or `python-libmagic`. Installing `python-magic` alongside these can lead to import conflicts or unexpected behavior due to incompatible APIs, particularly on Linux distributions like Fedora.
- gotcha The `python-magic` package is a wrapper around the C `libmagic` library. It does NOT automatically install the underlying system library. You must install `libmagic` separately via your operating system's package manager.
- gotcha If `libmagic` cannot find its magic database files (e.g., `MagicException: could not find any magic files!`), it means the library is installed but misconfigured.
- gotcha On Windows, attempting to use 32-bit `libmagic` DLLs with a 64-bit Python installation will result in a `WindowsError: [Error 193] %1 is not a valid Win32 application`.
- gotcha The `magic.Magic` class, which provides more direct control over `libmagic` functionality, is explicitly noted as 'not safe for sharing across multiple threads'.
Install
-
pip install python-magic -
sudo apt-get install libmagic1 -
brew install libmagic -
pip install python-magic-bin
Imports
- magic
import magic
Quickstart
import magic
import os
# Create a dummy file for demonstration
with open('test_file.txt', 'w') as f:
f.write('Hello, python-magic world!')
# Identify file type from a file path
file_type = magic.from_file('test_file.txt')
print(f"File type of 'test_file.txt': {file_type}")
# Identify MIME type from a file path
mime_type = magic.from_file('test_file.txt', mime=True)
print(f"MIME type of 'test_file.txt': {mime_type}")
# Identify file type from a buffer
with open('test_file.txt', 'rb') as f:
buffer_content = f.read(2048) # Recommend reading at least the first 2048 bytes
buffer_type = magic.from_buffer(buffer_content)
print(f"File type from buffer: {buffer_type}")
# Clean up the dummy file
os.remove('test_file.txt')
# Using the Magic class for more control (e.g., specifying magic database file)
# Note: The Magic class is not safe for sharing across multiple threads
# f = magic.Magic(uncompress=True)
# decompressed_type = f.from_file('testdata/test.gz')
# print(f"Decompressed type: {decompressed_type}")