Function which determines if a given file is binary.
Test is based on the following algorithm (similar to that implemented within Perl):
- Empty files are considered text.
- If not empty, read up to 512 bytes as a buffer. File will be binary if:
- Null byte is encountered.
- More than 30% of the buffer consists of "non text" characters.
- Otherwise, file is text.
Python 3.12.3 raises a
UnicodeDecodeError
exception when reading a binary file opened with'r'
. Here's my no OOP approach:Empty files reported as non-binary.
On a side note, please consider the humungous difference in simplicity, code transparency, ease of use, etc, etc, between obsessing in doing classes for every single thing or just coding it directly. If all you know is OOP, what you do every day is adjusting your problem to a predetermined solution.