You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将.txt文件部署到PythonAnywhere?Flask应用遇文件找不到错误

Fixing the "FileNotFoundError" for your words file on PythonAnywhere

Hey there, let's work through why your app can't find that words file—it's a super common issue when deploying Flask apps to PythonAnywhere, and we'll get it sorted quickly.

First, check for a filename typo (most likely culprit!)

You mentioned your app root has a word.txt file, but looking at your code:

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

Notice the difference? Your code is looking for words.txt (with an "s" at the end) but your actual file is named word.txt.

PythonAnywhere runs on a Linux environment, which is case-sensitive and exact about filenames. This mismatch is almost certainly causing the error. Fix this by either:

  • Renaming your file to words.txt in the PythonAnywhere file manager, or
  • Changing the string in your code to "word.txt" to match your actual filename.

If the typo isn't the issue: Use an absolute file path

Sometimes, Flask's working directory on PythonAnywhere isn't exactly your app's root folder, so relative paths like "words.txt" might not point where you expect. To make the file lookup reliable, use Python's os module to build an absolute path based on your app's files.

Step 1: Import the os module

Add this at the top of your Python file:

import os

Step 2: Update your Boggle class to use the absolute path

Modify the __init__ method to generate the correct file path automatically:

class Boggle():
    def __init__(self):
        # Get the full path of the directory containing this file
        app_dir = os.path.dirname(os.path.abspath(__file__))
        # Build the full path to your words file (adjust the filename if needed!)
        dict_path = os.path.join(app_dir, "words.txt")
        self.words = self.read_dict(dict_path)
    
    # ... rest of your class methods stay the same

Bonus tip: I'd also recommend using a with statement in your read_dict method—it automatically closes the file, which is cleaner and safer than manual close() calls:

def read_dict(self, dict_path):
    """Read and return all words in dictionary."""
    with open(dict_path) as dict_file:
        words = [w.strip() for w in dict_file]
    return words

Double-check the file's location in PythonAnywhere

Head to the PythonAnywhere file manager and confirm your .txt file is in the same directory as your main Flask app file (like app.py or run.py). Sometimes uploads can accidentally end up in subfolders, or fail silently.

You can also verify via the PythonAnywhere console:

  1. Log into your PythonAnywhere account and open a Bash console
  2. Navigate to your app folder with: cd /home/your_username/your_app_directory
  3. Run ls to list all files—you should see your .txt file here.

内容的提问来源于stack exchange,提问作者rudehlabya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:39:50