binaryornot Library Entry
binaryornot is an ultra-lightweight, pure Python package designed to check if a file is binary or text using simple heuristics. It is currently at version 0.6.0 and appears to have a slow but steady release cadence, primarily for Python version compatibility updates.
Warnings
- gotcha The `is_binary` function uses heuristics (checking for null bytes and specific control characters within the initial file chunk) rather than a definitive file format analysis. This means it may occasionally misclassify certain text files with unusual encodings or binary files lacking typical binary signatures in the sampled portion.
- gotcha The `is_binary` function directly interacts with the filesystem. It will raise standard Python I/O exceptions such as `FileNotFoundError` if the specified path does not exist, or `PermissionError` if the file cannot be opened for reading.
- gotcha Incorrect import path: A common mistake is to attempt to import `is_binary` directly from the top-level `binaryornot` package. However, the function resides in the `binary` submodule.
Install
-
pip install binaryornot
Imports
- is_binary
from binaryornot.binary import is_binary
Quickstart
import os
import tempfile
from binaryornot.binary import is_binary
# Create a dummy text file
with tempfile.NamedTemporaryFile(delete=False, mode='w', encoding='utf-8') as f:
f.write('This is a text file.\nHello World.')
text_filepath = f.name
# Create a dummy binary file (e.g., with null bytes)
with tempfile.NamedTemporaryFile(delete=False, mode='wb') as f:
f.write(b'\x00\x01\x02\x03This is a binary file.')
binary_filepath = f.name
# Check the files
print(f"'{text_filepath}' is binary: {is_binary(text_filepath)}")
print(f"'{binary_filepath}' is binary: {is_binary(binary_filepath)}")
# Clean up temporary files
os.remove(text_filepath)
os.remove(binary_filepath)