hfutils.utils.binary
This module provides functionality to determine whether a given file is a binary file or a text file. It does so by reading the first 1024 bytes of the file and checking for the presence of non-text characters.
Note
Inspired from https://stackoverflow.com/a/7392391/6995899
is_binary_file
- hfutils.utils.binary.is_binary_file(file) bool [source]
Check if a given file is binary.
This function reads the first 1024 bytes of the file and checks if it contains any non-text (binary) characters. It uses a predefined set of text characters to determine the nature of the file.
- Parameters:
file (str) – The path to the file to be checked.
- Returns:
True if the file is binary, False if it is a text file.
- Return type:
bool
- Raises:
FileNotFoundError – If the specified file does not exist.
IOError – If there is an error reading the file.
- Example:
>>> is_binary_file('example.txt') False >>> is_binary_file('example.bin') True