Skip to main content

Ultra-lightweight pure Python package to check if a file is binary or text.

Project description

BinaryOrNot

Python library and CLI tool to check if a file is binary or text. Zero dependencies.

from binaryornot.check import is_binary

is_binary("image.png")    # True
is_binary("README.md")    # False
is_binary("data.sqlite")  # True
is_binary("report.csv")   # False
$ binaryornot image.png
True

Install

pip install binaryornot

Why not just check for null bytes?

That's the first thing everyone tries. It works until it doesn't:

  • A UTF-16 text file is full of null bytes. Your tool thinks it's binary and corrupts it.
  • A Big5 or GB2312 text file has high-ASCII bytes everywhere. Looks binary by byte ratios alone.
  • A font file (.woff, .eot) is clearly binary but might not have null bytes in the first chunk.

BinaryOrNot reads the first 128 bytes and runs them through a trained decision tree that considers byte ratios, Shannon entropy, encoding validity, BOM detection, and more. It handles all the edge cases above correctly, with zero dependencies.

Tested against 37 text encodings and 49 binary formats, verified by parametrized tests driven from coverage CSVs.

API

One function:

from binaryornot.check import is_binary

is_binary(filename)  # returns True or False

There's also is_binary_string() if you already have bytes:

from binaryornot.helpers import is_binary_string

is_binary_string(b"\x00\x01\x02")  # True
is_binary_string(b"hello world")   # False

Full documentation covers the detection algorithm in detail.

Credits

Created by Audrey Roy Greenfeld.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binaryornot-0.6.0.tar.gz (478.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

binaryornot-0.6.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file binaryornot-0.6.0.tar.gz.

File metadata

  • Download URL: binaryornot-0.6.0.tar.gz
  • Upload date:
  • Size: 478.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for binaryornot-0.6.0.tar.gz
Algorithm Hash digest
SHA256 cc8d57cfa71d74ff8c28a7726734d53a851d02fad9e3a5581fb807f989f702f0
MD5 a809b2e789aed22876a2bcc71f765713
BLAKE2b-256 86724755b85101f37707c71526a301c1203e413c715a0016ecb592de3d2dcfff

See more details on using hashes here.

Provenance

The following attestation bundles were made for binaryornot-0.6.0.tar.gz:

Publisher: publish.yml on binaryornot/binaryornot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binaryornot-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: binaryornot-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for binaryornot-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 900adfd5e1b821255ba7e63139b0396b14c88b9286e74e03b6f51e0200331337
MD5 aa21f229986db26be6d7c50ebc44f924
BLAKE2b-256 cd0c31cfaa6b56fe23488ecb993bc9fc526c0d84d89607decdf2a10776426c2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for binaryornot-0.6.0-py3-none-any.whl:

Publisher: publish.yml on binaryornot/binaryornot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page