Small and dependency-free Python package to infer file type and MIME type checking the magic numbers signature of a file or buffer.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for managing and querying the type matcher system, including support for custom type matchers and type lookup by MIME type or extension.
Function to find type instances by searching for specific MIME types or file extensions.
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.
"""Usage Examples:
import filetype
# Find type by extension
jpeg_type = filetype.get_type(ext='jpg')
if jpeg_type:
print(f'MIME: {jpeg_type.mime}') # 'image/jpeg'
# Find type by MIME
pdf_type = filetype.get_type(mime='application/pdf')
if pdf_type:
print(f'Extension: {pdf_type.extension}') # 'pdf'
# Type not found
unknown = filetype.get_type(ext='unknown')
print(unknown) # None
# Use for validation
def is_supported_extension(ext):
return filetype.get_type(ext=ext) is not None
print(is_supported_extension('png')) # True
print(is_supported_extension('xyz')) # FalseFunction to add custom type matchers to extend the package's detection capabilities.
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
"""Usage Examples:
import filetype
from filetype.types.base import Type
# Define a custom type matcher
class CustomType(Type):
"""Custom file type matcher."""
def __init__(self):
super(CustomType, self).__init__(
mime='application/x-custom',
extension='custom'
)
def match(self, buf):
"""Check if buffer matches custom format."""
return (len(buf) >= 4 and
buf[0:4] == b'CUST')
# Register the custom type
custom_matcher = CustomType()
filetype.add_type(custom_matcher)
# Now the custom type can be detected
test_data = b'CUST\x00\x01\x02\x03'
kind = filetype.guess(test_data)
if kind:
print(f'Extension: {kind.extension}') # 'custom'
print(f'MIME: {kind.mime}') # 'application/x-custom'
# Custom types are added to the beginning of the search list
# They take precedence over built-in typesAccess to the complete list of supported type matcher instances.
types: listUsage Examples:
import filetype
# Access all supported types
all_types = filetype.types
print(f'Total supported types: {len(all_types)}')
# List all image types
image_types = [t for t in filetype.types if 'image/' in t.mime]
print('Supported image formats:')
for img_type in image_types:
print(f' .{img_type.extension} ({img_type.mime})')
# List all supported extensions
extensions = [t.extension for t in filetype.types]
print('All supported extensions:', sorted(set(extensions)))
# Find types by category
def get_types_by_category(category):
"""Get all types matching a MIME category."""
return [t for t in filetype.types if t.mime.startswith(f'{category}/')]
video_types = get_types_by_category('video')
audio_types = get_types_by_category('audio')All type matchers inherit from the base Type class:
class Type:
"""
Represents the file type object inherited by specific file type matchers.
Provides convenient accessor and helper methods.
"""
def __init__(self, mime: str, extension: str):
"""
Initialize type with MIME type and extension.
Args:
mime (str): MIME type string (e.g., 'image/jpeg')
extension (str): File extension string (e.g., 'jpg')
"""
@property
def mime(self) -> str:
"""
Get MIME type string.
Returns:
MIME type string (e.g., 'image/jpeg', 'video/mp4')
"""
@property
def extension(self) -> str:
"""
Get file extension string.
Returns:
File extension without dot (e.g., 'jpg', 'png', 'mp4')
"""
def is_extension(self, extension: str) -> bool:
"""
Check if this type matches the given extension.
Args:
extension (str): Extension to check
Returns:
True if extension matches this type's extension
"""
def is_mime(self, mime: str) -> bool:
"""
Check if this type matches the given MIME type.
Args:
mime (str): MIME type to check
Returns:
True if MIME type matches this type's MIME type
"""
def match(self, buf: bytearray) -> bool:
"""
Check if buffer matches this file type.
Must be implemented by subclasses.
Args:
buf (bytearray): File header bytes (first 8192 bytes)
Returns:
True if bytes match this file type, False otherwise
Raises:
NotImplementedError: If not implemented by subclass
"""To create custom type matchers, inherit from the base Type class:
from filetype.types.base import Type
class MyCustomType(Type):
"""Example custom type implementation."""
MIME = 'application/x-mycustom'
EXTENSION = 'mcf'
def __init__(self):
super(MyCustomType, self).__init__(
mime=self.MIME,
extension=self.EXTENSION
)
def match(self, buf):
"""
Implement magic number detection logic.
Args:
buf: bytearray containing first 8192 bytes of file
Returns:
bool: True if buffer matches this file type
"""
# Example: check for specific magic bytes
return (len(buf) >= 8 and
buf[0:4] == b'MCST' and # Magic signature
buf[4:8] == b'\x01\x00\x00\x00') # Version# Register custom types early in your application
def register_custom_types():
"""Register all custom type matchers."""
# Custom types are checked first (LIFO order)
filetype.add_type(MyCustomType())
filetype.add_type(AnotherCustomType())
# Call during application initialization
register_custom_types()
# Verify registration
custom_type = filetype.get_type(ext='mcf')
if custom_type:
print('Custom type registered successfully')Utility functions to check if specific MIME types or extensions are supported:
def is_extension_supported(ext: str) -> bool:
"""
Checks if the given extension string is one of the supported by the file matchers.
Args:
ext: file extension string. E.g: jpg, png, mp4, mp3
Returns:
True if the file extension is supported. Otherwise False.
"""
def is_mime_supported(mime: str) -> bool:
"""
Checks if the given MIME type string is one of the supported by the file matchers.
Args:
mime: MIME string. E.g: image/jpeg, video/mpeg
Returns:
True if the MIME type is supported. Otherwise False.
"""Usage Examples:
import filetype
# Check extension support
if filetype.is_extension_supported('pdf'):
print('PDF files are supported')
# Check MIME support
if filetype.is_mime_supported('image/webp'):
print('WebP images are supported')
# Use for input validation
def validate_file_type(filename):
"""Validate if file type is supported."""
ext = filename.split('.')[-1].lower()
if filetype.is_extension_supported(ext):
return True
else:
print(f'Unsupported file type: .{ext}')
return False
# Batch validation
supported_files = []
for filename in ['doc.pdf', 'image.jpg', 'data.xyz']:
if validate_file_type(filename):
supported_files.append(filename)Install with Tessl CLI
npx tessl i tessl/pypi-filetype