Getting Started¶
This tutorial takes you from installation to the first successful file type detection.
Installation¶
Using pip¶
Using rye¶
If you're using rye for dependency management:
Interactive terminal demo¶
Browse any directory with a live filename filter and side-by-side strategy results:
python -m filetype_detector path/to/files launches the same interface. Omit
the directory to browse the current working directory.
System Requirements¶
Python¶
- Python >= 3.10
System Libraries¶
Important: MagicInferencer and HybridInferencer require the libmagic system library. Install it based on your operating system:
Ubuntu/Debian¶
Fedora/RHEL/CentOS¶
Arch Linux¶
macOS¶
Using Homebrew (Recommended):
Using MacPorts:
Windows¶
Windows doesn't have native libmagic support. Use python-magic-bin:
Alternatively, download the libmagic DLL manually from:
- file.exe Windows releases
Alpine Linux (Common in Docker)¶
Verify Installation¶
After installation, verify libmagic is available:
You should see output like: file-5.x
If this command works, libmagic is properly installed and MagicInferencer will work correctly.
Basic Usage¶
Recommended: HybridInferencer¶
For most use cases, start with AutoInferencer(backend="hybrid") - it provides a single entry point with the same balanced behavior as HybridInferencer:
Content-based backends require the supplied path to reference an existing file.
from filetype_detector import AutoInferencer
inferencer = AutoInferencer(backend="hybrid")
file_type = inferencer.infer("document.pdf")
'.pdf' in file_type.extensions # True
Using Individual Inferencers¶
You can also use inferencer classes directly:
from filetype_detector import MagicInferencer
inferencer = MagicInferencer()
file_type = inferencer.infer("document.pdf")
print(file_type.extensions) # Output: ('.pdf',)
Using AutoInferencer¶
For type-safe backend selection, use AutoInferencer:
from filetype_detector import AutoInferencer
magic = AutoInferencer(backend="magic")
file_type = magic.infer("file_without_ext")
print(file_type.extensions)
See Examples and Patterns for longer examples and AutoInferencer for backend selection details.
Next Steps¶
- Read the How-to Guides when you want to solve a specific task
- Browse Examples and Patterns for reusable snippets and larger examples
- Use the Reference Overview when you need exact API details
- Read the Explanation Overview for design rationale and trade-offs