Small and dependency-free Python package to infer file type and MIME type checking the magic numbers signature of a file or buffer.
npx @tessl/cli install tessl/pypi-filetype@1.2.0A small and dependency-free Python package to infer file type and MIME type by checking the magic numbers signature of a file or buffer. The package provides high-performance file type detection for 80+ file formats across multiple categories without requiring external dependencies.
pip install filetypeimport filetypeCommon usage patterns:
from filetype import guess, guess_mime, guess_extensionFor category-specific operations:
from filetype import is_image, is_video, is_audioimport filetype
# Basic file type detection from file path
kind = filetype.guess('/path/to/file.jpg')
if kind is None:
print('Cannot guess file type!')
else:
print('File extension: %s' % kind.extension)
print('File MIME type: %s' % kind.mime)
# Detection from bytes buffer
with open('/path/to/file.jpg', 'rb') as f:
header = f.read(261) # Read first 261 bytes
kind = filetype.guess(header)
print('File type:', kind.extension if kind else 'Unknown')
# Direct MIME type detection
mime = filetype.guess_mime('/path/to/file.pdf')
print('MIME type:', mime) # 'application/pdf'
# Direct extension detection
ext = filetype.guess_extension('/path/to/file.png')
print('Extension:', ext) # 'png'The filetype package is built around a modular type matcher system:
Type class with specialized implementations for each file formatadd_type() functionThe package supports multiple input types (file paths, bytes, bytearray, file-like objects) and provides both general detection and category-specific matching functions.
The package exposes several module-level constants and variables for advanced usage:
types: List[Type]
"""List of all supported type matcher instances. Allows iteration over all file types."""
__version__: str
"""Package version string (e.g., '1.2.0')."""
version: str
"""Package version string (alias for __version__)."""Usage Examples:
import filetype
# Get package version
print(f'filetype version: {filetype.__version__}')
# Iterate over all supported types
for file_type in filetype.types:
print(f'{file_type.extension} -> {file_type.mime}')
# Filter types by category
image_types = [t for t in filetype.types if 'image/' in t.mime]
print(f'Supported image formats: {len(image_types)}')Primary file type detection functionality that analyzes magic number signatures to identify file types and return structured type information.
def guess(obj):
"""
Infers the type of the given input.
Args:
obj: path to file, bytes or bytearray.
Returns:
The matched type instance. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""
def guess_mime(obj):
"""
Infers the file type and returns its MIME type.
Args:
obj: path to file, bytes or bytearray.
Returns:
The matched MIME type as string. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""
def guess_extension(obj):
"""
Infers the file type and returns its file extension.
Args:
obj: path to file, bytes or bytearray.
Returns:
The matched file extension as string. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""Functions for managing and querying the type matcher system, including support for custom type matchers and type lookup by MIME type or extension.
def get_type(mime=None, ext=None):
"""
Returns the file type instance searching by MIME type or file extension.
Args:
ext: file extension string. E.g: jpg, png, mp4, mp3
mime: MIME string. E.g: image/jpeg, video/mpeg
Returns:
The matched file type instance. Otherwise None.
"""
def add_type(instance):
"""
Adds a new type matcher instance to the supported types.
Args:
instance: Type inherited instance.
Returns:
None
Raises:
TypeError: if instance doesn't inherit from filetype.types.Type
"""High-level functions for checking if a file belongs to specific categories like images, videos, or documents, providing quick boolean results for common use cases. Also includes utility functions for checking extension and MIME type support.
def is_extension_supported(ext):
"""
Checks if the given extension string is supported by the file matchers.
Args:
ext (str): file extension string. E.g: jpg, png, mp4, mp3
Returns:
True if the file extension is supported. Otherwise False.
"""
def is_mime_supported(mime):
"""
Checks if the given MIME type string is supported by the file matchers.
Args:
mime (str): MIME string. E.g: image/jpeg, video/mpeg
Returns:
True if the MIME type is supported. Otherwise False.
"""
def is_image(obj):
"""
Checks if a given input is a supported type image.
Args:
obj: path to file, bytes or bytearray.
Returns:
True if obj is a valid image. Otherwise False.
Raises:
TypeError: if obj is not a supported type.
"""
def is_video(obj):
"""
Checks if a given input is a supported type video.
Args:
obj: path to file, bytes or bytearray.
Returns:
True if obj is a valid video. Otherwise False.
Raises:
TypeError: if obj is not a supported type.
"""
def is_audio(obj):
"""
Checks if a given input is a supported type audio.
Args:
obj: path to file, bytes or bytearray.
Returns:
True if obj is a valid audio. Otherwise False.
Raises:
TypeError: if obj is not a supported type.
"""Advanced matching functions that search within specific file type categories, providing more targeted detection when you know the expected file category.
def image_match(obj):
"""
Matches the given input against the available image type matchers.
Args:
obj: path to file, bytes or bytearray.
Returns:
Type instance if matches. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""
def video_match(obj):
"""
Matches the given input against the available video type matchers.
Args:
obj: path to file, bytes or bytearray.
Returns:
Type instance if matches. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""
def audio_match(obj):
"""
Matches the given input against the available audio type matchers.
Args:
obj: path to file, bytes or bytearray.
Returns:
Type instance if matches. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""The package supports 80+ file formats across 7 categories:
The package includes a command-line interface for file type detection:
# Install provides the 'filetype' command
pip install filetype
# Check file types
filetype -f file1.jpg file2.png file3.pdf
# Show version
filetype --version