hbutils.system.filesystem.binary

Binary and text file detection utilities.

This module provides a lightweight heuristic for determining whether a file should be treated as binary or text. It samples the initial portion of a file and checks for the presence of non-text byte values. The main public helpers are is_binary_file() and is_text_file().

The module contains the following main components:

Note

Empty files are treated as text files by both public helpers.

Example:

>>> from hbutils.system.filesystem.binary import is_binary_file, is_text_file
>>> is_binary_file('README.md')
False
>>> is_text_file('README.md')
True

__all__

hbutils.system.filesystem.binary.__all__ = ['is_binary_file', 'is_text_file']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

is_binary_file

hbutils.system.filesystem.binary.is_binary_file(filename: str | PathLike) bool[source]

Check if the given file is a binary file.

This function reads a sample from the beginning of the file and checks if it contains binary characters. Files are considered binary if they contain characters outside the standard text character set.

Parameters:

filename (str or os.PathLike) – The path to the file to check.

Returns:

True if the file is binary, False otherwise.

Return type:

bool

Examples:

>>> from hbutils.system import is_binary_file
>>> is_binary_file('rar_template-simple.rar')
True
>>> is_binary_file('README.md')
False

Note

Empty files will be treated as text files.

is_text_file

hbutils.system.filesystem.binary.is_text_file(filename: str | PathLike) bool[source]

Check if the given file is a text file.

This function reads a sample from the beginning of the file and checks if it contains only text characters. Files are considered text if they do not contain binary characters.

Parameters:

filename (str or os.PathLike) – The path to the file to check.

Returns:

True if the file is text, False otherwise.

Return type:

bool

Examples:

>>> from hbutils.system import is_text_file
>>> is_text_file('rar_template-simple.rar')
False
>>> is_text_file('README.md')
True

Note

Empty files will be treated as text files.