python - 如何从 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”文件位于目录的根目录中。
解决方案
推荐阅读
- list - 多行文本在 SwiftUI 列表内的 NavigationLink 中不起作用
- python - 每次我运行 Selenium 来 ping 一个特定站点时,我的 WebDriver 最终都会转到一个完全不同的 URL
- asp.net-core - ASP.Net.Core.Identity 2.0 上的 FirstName 和 LastName 以及其他字段
- typescript - Boolean('false') 返回真
- google-cloud-storage - 使用客户管理的加密密钥时,Google 存储操作极其缓慢
- excel - 如何使用 Excel 宏将文本添加到文件名
- python - 从 html 运行 web 脚本的任何方式,该浏览器在页面下载时自动运行?
- asp.net-core - 值不能为空。参数名称:发布部署到CentOS服务器后的connectionString
- reactjs - 主题对象空
- sql-server - SQL Server 局部变量导致性能问题