Registry / serialization / filetype

filetype

JSON →
library1.2.0pypypi✓ verified 26d ago

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.

pip install filetype
INSTALL
IMPORT
SIG · FILETYPE
F
filetype
serializationpythonv1.2.0
Install
1.6s avg
Import
22ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.024s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.020s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

filetype
import filetype

This quickstart demonstrates how to use `filetype.guess()` to infer the type of a file based on its path and from a bytes buffer. It creates a dummy JPEG file to showcase the functionality and then cleans it up. The library returns a `Kind` object with `extension` and `mime` attributes, or `None` if the type cannot be determined.

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)
Debug
Known issues
gotchaThe `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.
fix
Implement a check for `if kind is None:` or similar logic to handle unknown file types gracefully.
affects: All versions
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.
fix
Use `try-except FileNotFoundError` blocks to handle missing files, or verify file existence using `os.path.exists()` before calling `guess()`.
affects: All versions
gotchaWhen 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.
fix
Ensure that the buffer passed to `filetype.guess()` contains at least the initial bytes of the file, ideally up to 261 bytes, for reliable type detection.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'filetype'
The 'filetype' package is not installed in your Python environment.
fix
You need to install the package using pip: `pip install filetype`
filetype.guess() returns None
The 'filetype' library could not infer the file type from its magic numbers. This often happens if the file is corrupted, empty, or its type is not supported by the library (version 1.2.0).
fix
Ensure the file is valid and supported. If reading from a buffer, ensure enough bytes are provided. Handle the `None` return gracefully: 
```python
import filetype

kind = filetype.guess('path/to/your/file')
if kind is None:
    print('File type could not be determined or is not supported.')
else:
    print(f'File extension: {kind.extension}')
    print(f'File MIME type: {kind.mime}')
```
FileNotFoundError: [Errno 2] No such file or directory: 'your_file_name'
The specified file path for `filetype.guess()` or file operations does not point to an existing file.
fix
Verify that the file path is correct and that the file exists at the given location. Use an absolute path or ensure the script is run from the correct directory. 
```python
import filetype
import os

file_path = 'path/to/correct/file.jpg'
if os.path.exists(file_path):
    kind = filetype.guess(file_path)
    if kind:
        print(f'Detected type: {kind.mime}')
    else:
        print('File type not recognized.')
else:
    print(f'Error: File not found at {file_path}')
```
Upgrade
Version history
1.2.0latest on PyPI · released Nov 2, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
filetype — pip install filetype · libregistry