def read_file_content()

in azext_edge/edge/util/file_operations.py [0:0]


def read_file_content(file_path: str, read_as_binary: bool = False) -> Union[bytes, str]:
    from pathlib import Path

    logger.debug("Processing %s", file_path)
    pure_path = Path(os.path.abspath(os.path.expanduser(file_path)))

    if not pure_path.exists():
        raise FileOperationError(f"{file_path} does not exist.")

    if not pure_path.is_file():
        raise FileOperationError(f"{file_path} is not a file.")

    if read_as_binary:
        logger.debug("Reading %s as binary", file_path)
        return pure_path.read_bytes()

    # Try with 'utf-8-sig' first, so that BOM in WinOS won't cause trouble.
    for encoding in ["utf-8-sig", "utf-8"]:
        try:
            logger.debug("Reading %s as %s", file_path, encoding)
            return pure_path.read_text(encoding=encoding)
        except (UnicodeError, UnicodeDecodeError):
            pass

    raise FileOperationError(f"Failed to decode file {file_path}.")