import pathlib


def write_bytes(path: str, data: bytes, append=False):
    """write binary `data` to path `path`

    If file does not exist, it creates a new file. If file exists it truncates the file unless append arg is True
    e.g
        j.sals.fs.write_bytes(path="/home/rafy/testing_text.txt",data=b"hello world")  -> 11

    Args:
        path (str): path to write to
        data (bytes): binary content
        append (bool, optional): indicate whether to open the file in append mode or not.

    Returns:
        int:  returning the number of characters written.
    """
    p = pathlib.Path(path)
    mode = "ab" if append else "wb"
    with p.open(mode=mode) as f:
        return f.write(data)