Ultra-lightweight pure Python package to check if a file is binary or text.
Project description
Ultra-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.
Free software: BSD license
Documentation: https://binaryornot.readthedocs.io
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: http://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.
Builds
Linux (Ubuntu 12.04 LTS Server Edition 64 bit):
Windows (Windows Server 2012 R2 (x64)):
Credits
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
History
0.4.4 (2017-04-13)
Notify users for file i/o issues. Thanks @lukehinds!
0.4.3 (2017-04-13)
Restricted chardet to anything 3.0.2 or higher due to https://github.com/chardet/chardet/issues/113. Thanks @dan-blanchard for the quick fix!
0.4.2 (2017-04-12)
Restricted chardet to anything under 3.0 due to https://github.com/chardet/chardet/issues/113
Added pyup badge
Added utilities for pushing new versions up
0.4.0 (2015-08-21)
Enhanced detection for some binary streams and UTF texts. (#10, 11) Thanks @pombredanne.
Set up Appveyor for continuous testing on Windows. Thanks @pydanny.
Update link to Perl source implementation. (#9) Thanks @asmeurer @pombredanne @audreyr.
Handle UnicodeDecodeError in check. (#12) Thanks @DRMacIver.
Add very simple Hypothesis based tests. (#13) Thanks @DRMacIver.
Use setup to determine requirements and remove redundant requirements.txt. (#14) Thanks @hackebrot.
Add documentation status badge to README.rst. (#15) Thanks @hackebrot.
Run tox in travis.yml. Add pypy and Python 3.4 to tox environments. (#16) Thanks @hackebrot @pydanny.
Handle LookupError when detecting encoding. (#17) Thanks @DRMacIver.
0.3.0 (2014-05-05)
Include tests, docs in source package. (#6) Thanks @vincentbernat.
Drop unnecessary shebangs and executable bits. (#8) Thanks @scop.
Generate string of printable extended ASCII bytes only once. (#7) Thanks @scop.
Make number of bytes to read parametrizable. (#7) Thanks @scop.
0.2.0 (2013-09-22)
Complete rewrite of everything. Thanks @ncoghlan.
0.1.1 (2013-08-17)
Tests pass under Python 2.6, 2.7, 3.3, PyPy.
0.1.0 (2013-08-17)
First release on PyPI.
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
Built Distribution
File details
Details for the file binaryornot-0.4.4.tar.gz
.
File metadata
- Download URL: binaryornot-0.4.4.tar.gz
- Upload date:
- Size: 371.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 359501dfc9d40632edc9fac890e19542db1a287bbcfa58175b66658392018061 |
|
MD5 | 09c0b7f5f3f7c881e2f306780eac7128 |
|
BLAKE2b-256 | a7fe7ebfec74d49f97fc55cd38240c7a7d08134002b1e14be8c3897c0dd5e49b |
File details
Details for the file binaryornot-0.4.4-py2.py3-none-any.whl
.
File metadata
- Download URL: binaryornot-0.4.4-py2.py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8b71173c917bddcd2c16070412e369c3ed7f0528926f70cac18a6c97fd563e4 |
|
MD5 | 7a69d00f3c384693c9616c3381c411a3 |
|
BLAKE2b-256 | 247ef7b6f453e6481d1e233540262ccbfcf89adcd43606f44a028d7f5fae5eb2 |