from pathlib import Path


def load_txt_to_string(file_path):
    """
    Loads a txt file and returns a str representation of it.

    :param file_path: str or Path object
    :return: The file's text as a string

    >>> from pathlib import Path
    >>> from gender_analysis import common
    >>> novel_path = Path(common.TEST_DATA_PATH, 'sample_novels', 'texts', 'austen_persuasion.txt')
    >>> novel_text = load_txt_to_string(novel_path)
    >>> type(novel_text), len(novel_text)
    (<class 'str'>, 486253)

    """
    if isinstance(file_path, str):
        file_path = Path(file_path)

    file_type = file_path.suffix

    if file_type != '.txt':
        raise Exception(
            'Cannot load if current file type is not .txt'
        )
    else:
        try:
            file = open(file_path, encoding='utf-8')
            result = file.read()
        except UnicodeDecodeError as err:
            print(f'Unicode file loading error {file_path}.')
            raise err

    file.close()
    return result