chunklet.common.path_utils
Functions:
-
is_binary_file–Determine whether a file is binary or text.
-
is_path_like–Check if a string looks like a filesystem path (file or folder),
-
read_text_file–Read text file with automatic encoding detection.
is_binary_file
Determine whether a file is binary or text.
First tries to guess the file type based on its MIME type derived from
the file extension. If MIME type is unavailable or ambiguous, reads the
first 1024 bytes of the file and checks for null bytes (b' '), which
indicate binary content.
Parameters:
-
(pathstr | Path) –Path to the file.
Returns:
-
bool–True if the file is likely binary, False if text.
Source code in src/chunklet/common/path_utils.py
is_path_like
Check if a string looks like a filesystem path (file or folder), including Unix/Windows paths, hidden files, and scripts without extensions.
Parameters:
-
(textstr) –text to check.
Returns:
-
bool–True if string appears to be a filesystem path.
Examples:
>>> is_path_like("/home/user/document.txt")
True
>>> is_path_like("C:\Users\User\file.pdf")
True
>>> is_path_like("folder/subfolder/script.sh")
True
>>> is_path_like(".hidden_file")
True
>>> is_path_like("no_extension_script")
True
>>> is_path_like("path/with/newline\nchar")
False
>>> is_path_like("string_with_null_byte\x00")
False
Source code in src/chunklet/common/path_utils.py
read_text_file
Read text file with automatic encoding detection.
Parameters:
-
(pathstr | Path) –File path to read.
Returns:
-
str–File content.
Raises:
-
FileProcessingError–If file cannot be read.