Skip to main content

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.

Metadata

Release files for binaryornot 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binaryornot 0.5.0
File Size Uploaded
binaryornot-0.5.0.tar.gz 428.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for binaryornot 0.5.0
File Interpreter ABI Platform
binaryornot-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 440.6 kB

Release files / binaryornot-0.5.0.tar.gz

Download URL binaryornot-0.5.0.tar.gz
Size 428.5 kB
Tags Source
SHA-256 checksum
How to use checksums
dcacb1343219da5fbbb7828a46b946768c6df07b65453195954cbbecf14c1c83
BLAKE2b-256 checksum
How to use checksums
617e8a41b27448bfcc8138f8aec3ac2a467edf22a1d85bdbbd7fd0a130372fc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 7, 2026.

Transparency log

Release files / binaryornot-0.5.0-py3-none-any.whl

Download URL binaryornot-0.5.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a969a893feb93508c14ea64a80354ba4a3164ccd2d3b5122cd438fab6965134
BLAKE2b-256 checksum
How to use checksums
771392fbe1fce5eecc0c926ec94a9a8904e9f9a74c286887205fbc071f4ea349
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 7, 2026.

Transparency log

Release history Release notifications | RSS feed

0.6.0

2 release files

This release

0.5.0 This release

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.0

2 release files

0.3.0

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page