{"id":1399,"library":"binaryornot","title":"binaryornot Library Entry","description":"binaryornot is an ultra-lightweight, pure Python package designed to check if a file is binary or text using simple heuristics. It is currently at version 0.6.0 and appears to have a slow but steady release cadence, primarily for Python version compatibility updates.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/audreyr/binaryornot","tags":["file","binary","text","utility","file-type"],"install":[{"cmd":"pip install binaryornot","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The `is_binary` function is located within the `binary` submodule, not directly under the top-level package.","wrong":"from binaryornot import is_binary","symbol":"is_binary","correct":"from binaryornot.binary import is_binary"}],"quickstart":{"code":"import os\nimport tempfile\n\nfrom binaryornot.binary import is_binary\n\n# Create a dummy text file\nwith tempfile.NamedTemporaryFile(delete=False, mode='w', encoding='utf-8') as f:\n    f.write('This is a text file.\\nHello World.')\n    text_filepath = f.name\n\n# Create a dummy binary file (e.g., with null bytes)\nwith tempfile.NamedTemporaryFile(delete=False, mode='wb') as f:\n    f.write(b'\\x00\\x01\\x02\\x03This is a binary file.')\n    binary_filepath = f.name\n\n# Check the files\nprint(f\"'{text_filepath}' is binary: {is_binary(text_filepath)}\")\nprint(f\"'{binary_filepath}' is binary: {is_binary(binary_filepath)}\")\n\n# Clean up temporary files\nos.remove(text_filepath)\nos.remove(binary_filepath)","lang":"python","description":"This quickstart demonstrates how to import and use the `is_binary` function to check both a generated text file and a generated binary file. It covers file creation, checking, and cleanup."},"warnings":[{"fix":"Understand that the detection is heuristic-based. For critical use cases where absolute certainty is required, consider using libraries that perform deeper file format analysis (e.g., python-magic) or explicit magic number checks if the expected format is known.","message":"The `is_binary` function uses heuristics (checking for null bytes and specific control characters within the initial file chunk) rather than a definitive file format analysis. This means it may occasionally misclassify certain text files with unusual encodings or binary files lacking typical binary signatures in the sampled portion.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always wrap calls to `is_binary` within `try...except` blocks to gracefully handle potential file system errors. Ensure the file path exists and the application has read permissions before invoking the function.","message":"The `is_binary` function directly interacts with the filesystem. It will raise standard Python I/O exceptions such as `FileNotFoundError` if the specified path does not exist, or `PermissionError` if the file cannot be opened for reading.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use the correct import statement: `from binaryornot.binary import is_binary`.","message":"Incorrect import path: A common mistake is to attempt to import `is_binary` directly from the top-level `binaryornot` package. However, the function resides in the `binary` submodule.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}