Skip to main content

isbinary

CI Coverage PyPI BSD License Documentation Status

Lightweight pure Python package to guess whether a file is binary or text, using a heuristic similar to Perl’s pp_fttext and its analysis by @eliben.

Status

It works, and people are using this package in various places. But it doesn’t cover all edge cases yet.

The code could be improved. Pull requests welcome! As of now, it is based on these snippets, but that may change:

Features

Has tests for these file types:

  • Text: .txt, .css, .json, .svg, .js, .lua, .pl, .rst

  • Binary: .png, .gif, .jpg, .tiff, .bmp, .DS_Store, .eot, .otf, .ttf, .woff, .rgb

Has tests for numerous encodings.

Why?

You may be thinking, “I can write this in 2 lines of code?!”

It’s actually not that easy. Here’s a great article about how Perl’s heuristic to guess file types works: https://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/

And that’s just where we started. Over time, we’ve found more edge cases and our heuristic has gotten more complex.

Also, this package saves you from having to write and thoroughly test your code with all sorts of weird file types and encodings, cross-platform.

History

This is a long-term fork of binaryornot. It was created in May 2022 primarily because it appeared that upstream had been abandoned. There were a few other smaller issues:

  1. Lack of type annotations.

  2. Lack of stricter modern code quality tools used in CI.

  3. Improved contributor experience by using Github Actions for CI.

  4. Possibility for optimisation with optional dependency on cchardet.

  5. Removal of Python 2 support, and explicit support for newer versions of Python 3.

Credits

  • Audrey and Danny Roy Greenfeld, as the previous maintainers of this code.

  • Special thanks to Eli Bendersky (@eliben) for his writeup explaining the heuristic and his implementation, which this is largely based on.

  • Source code from the portion of Perl’s pp_fttext that checks for textiness: https://github.com/Perl/perl5/blob/v5.23.1/pp_sys.c#L3527-L3587

Metadata

Release files for isbinary 1.0.1

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

Source distribution (sdist)

Source distribution for isbinary 1.0.1
File Size Uploaded
isbinary-1.0.1.tar.gz 6.9 kB Details

Built distribution (wheel)

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

Total release size: 13.8 kB

Release files / isbinary-1.0.1.tar.gz

Download URL isbinary-1.0.1.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
86b8ebc9df64408a1a75592361d2181a77558d545f9c80cdb81b187e30add883
BLAKE2b-256 checksum
How to use checksums
fe71336828d1ed240b8545c8d59f7995779adef3d3aeb4905c655060e7ba9aaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release files / isbinary-1.0.1-py3-none-any.whl

Download URL isbinary-1.0.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
708c86e931dc9cf117b5ef44983237ff879a2657142b08854ed05b8b81c67ed5
BLAKE2b-256 checksum
How to use checksums
bc0e5dd7fdd048078f97b1e39dafd0f7ecc5b0173f79aaef2ac46942765fc7bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

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