Filetype Inference Library
Small and dependency-free Python package to infer file type and MIME type by checking the magic numbers signature of a file or buffer. It is a Python port from the 'filetype' Go package, offering a simple and friendly API for a wide range of file types. Currently at version 1.2.0, it is actively maintained with periodic updates.
Warnings
- gotcha The `filetype.guess()` function returns `None` if it cannot determine the type of the given file or buffer. Always check for `None` before accessing `extension` or `mime` attributes.
- gotcha `filetype.guess()` will raise a `FileNotFoundError` if the provided file path does not exist. Ensure the file path is correct and the file is accessible.
- gotcha When guessing from a buffer, `filetype` typically only requires the first 261 bytes (maximum file header size) for accurate detection. Providing an insufficient number of bytes may lead to incorrect type inference or `None` being returned.
Install
-
pip install filetype
Imports
- filetype
import filetype
Quickstart
import filetype
import os
# Create a dummy JPEG file for demonstration
dummy_jpeg_path = 'dummy.jpg'
# A minimal JPEG header (first few bytes of a typical JPEG file)
# In a real scenario, you'd read from an actual file.
jpeg_magic_bytes = b'\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00\x01'
try:
with open(dummy_jpeg_path, 'wb') as f:
f.write(jpeg_magic_bytes)
# Guess the file type from the path
kind = filetype.guess(dummy_jpeg_path)
if kind is None:
print('Cannot guess file type!')
else:
print(f'File extension: {kind.extension}')
print(f'File MIME type: {kind.mime}')
# You can also guess from a buffer/bytes object
kind_from_buffer = filetype.guess(jpeg_magic_bytes)
if kind_from_buffer:
print(f'Buffer extension: {kind_from_buffer.extension}')
print(f'Buffer MIME type: {kind_from_buffer.mime}')
finally:
# Clean up the dummy file
if os.path.exists(dummy_jpeg_path):
os.remove(dummy_jpeg_path)