首页 > 解决方案 > 如何从 Python/Flask 应用程序将 .txt 文件部署到 PythonAnywhere

问题描述

我正在尝试将 Python/Flask 应用程序部署到 PythonAnywhere,但我收到“出现问题”错误。检查错误日志后,我发现此消息“FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'”。我的应用程序中有一个 word.txt 文件,其中包含各种不同的单词(我将它用作我的应用程序从中读取的字典)并且想知道如何从 PythonAnywhere 部署 .txt 文件?我上传的所有其他文件/文件夹似乎都被接受了。

这是我的代码:

Python

class Boggle():

    def __init__(self):

        self.words = self.read_dict("words.txt")

    def read_dict(self, dict_path):
        """Read and return all words in dictionary."""

        dict_file = open(dict_path)
        words = [w.strip() for w in dict_file]
        dict_file.close()
        return words

    def make_board(self):
        """Make and return a random boggle board."""

        board = []

        for y in range(5):
            row = [choice(string.ascii_uppercase) for i in range(5)]
            board.append(row)

        return board

    def check_valid_word(self, board, word):
        """Check if a word is a valid word in the dictionary and/or the boggle board"""

        word_exists = word in self.words
        valid_word = self.find(board, word.upper())

        if word_exists and valid_word:
            result = "ok"
        elif word_exists and not valid_word:
            result = "not-on-board"
        else:
            result = "not-word"

        return result

另外这里是我的目录的截图 项目目录的照片

如您所见,“word.txt”文件位于目录的根目录中。

标签: pythonflaskpythonanywheretxt

解决方案


推荐阅读