geekcomputers/Python · error · FileNotFoundError

File local_words.txt was not found

Error message

File local_words.txt was not found

What it means

parse_word_from_local re-raises FileNotFoundError with a clearer message when local_words.txt (expected next to the module, resolved via data_path) is missing. It is the file-based fallback word source for the industrial hangman game.

Source

Thrown at Industrial_developed_hangman/src/hangman/main.py:61

    print_function(Style.RESET_ALL + Fore.GREEN + text)


def parse_word_from_local(
    choice_function: Callable[[List[str]], str] = random.choice,
) -> str:
    # noqa: DAR201
    """
    Parse word from local file.

    :parameter choice_function: Function that will be used to choice a word from file.
    :returns str: string that contains the word.
    :raises FileNotFoundError: file to read words not found.
    """
    try:
        with open(data_path / "local_words.txt", encoding="utf8") as words_file:
            return choice_function(words_file.read().split("\n"))
    except FileNotFoundError:
        raise FileNotFoundError("File local_words.txt was not found")


def parse_word_from_site(
    url: str = "https://random-word-api.herokuapp.com/word",
) -> str:
    # noqa: DAR201
    """
    Parse word from website.

    :param url: url that word will be parsed from.
    :return Optional[str]: string that contains the word.
    :raises ConnectionError: no connection to the internet.
    :raises RuntimeError: something go wrong with getting the word from site.
    """
    try:
        response: requests.Response = requests.get(url, timeout=request_timeout)
    except ConnectionError:
        raise ConnectionError("There is no connection to the internet")

View on GitHub (pinned to 40f4cd2652)

Solutions

  1. Verify data_path resolves to src/hangman/data and create local_words.txt there
  2. Add the file to package data (pyproject/setup: package-data / MANIFEST.in) so installs include it
  3. Fall back to Source.FROM_INTERNET in get_word when the file is missing
  4. Catch FileNotFoundError at the call site and show a friendly message

Example fix

# before
word = get_word(Source.FROM_FILE)
# after
if not (data_path / 'local_words.txt').exists():
    word = get_word(Source.FROM_INTERNET)
else:
    word = get_word(Source.FROM_FILE)
Defensive patterns

Strategy: fallback

Validate before calling

from pathlib import Path
def local_words_available(data_path) -> bool:
    return (data_path / 'local_words.txt').exists()

Try / catch

try:
    word = get_word(Source.FROM_FILE)
except FileNotFoundError:
    word = get_word(Source.FROM_INTERNET)

Prevention

When it happens

Trigger: get_word called with Source.FROM_FILE when src/hangman/data/local_words.txt does not exist in the installed location; data_path pointing to a wrong directory; packaging that omits the data file.

Common situations: pip-installing the package without package_data/include-package-data configured so local_words.txt isn't shipped; running from a different working directory after a move; renaming the data folder.

Related errors


AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27). Data as JSON: /api/errors/5367b12b19239797. Report an issue: GitHub.