binaryornot Library Entry

0.6.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `is_binary` function to check both a generated text file and a generated binary file. It covers file creation, checking, and cleanup.

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)

view raw JSON →