python-magic

0.4.27 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

The quickstart demonstrates basic file and buffer type identification using `magic.from_file()` and `magic.from_buffer()`. It also shows how to get the MIME type. A dummy file is created and cleaned up for the example. Note the recommendation to read at least 2048 bytes for `from_buffer` for accurate identification.

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}")

view raw JSON →