【问题标题】:IsADirectoryError: [Errno 21] Is a directory: '/app' when deploying on HerokuIsADirectoryError: [Errno 21] Is a directory: \'/app\' 在 Heroku 上部署时
【发布时间】:2022-08-22 00:54:19
【问题描述】:

我在 Heroku 上部署 Flask 应用程序时遇到了问题。我当前的问题是 Heroku 无法在我的静态文件夹中找到文件。在尝试了关于 SO 的建议后,我现在收到此错误:

with open(file_dir, encoding=\"utf-8\") as f:
IsADirectoryError: [Errno 21] Is a directory: \'/app\' 

这是我发现的帮助 Heroku 找到鼓励.txt 文件的建议之一:

# Absolute path to the file
file_dir = os.path.dirname(os.path.abspath(\'encouragement.txt\'))
# Absolute path to this file\'s root directory
parent_dir = os.path.join(file_dir, os.pardir)
app_dir = os.path.join(parent_dir, \'/app\')

添加了 \'/app\' 是因为其中一篇文章提到 Heroku 有一个 app 目录作为父目录,所以不是 \'static/encouragement.txt\',而是 \'app/static/encouragement。文本\'。加入和不加入 \'/app\' 都会出现相同的错误。 这是错误的来源:

@app.route(\"/\")
def index():
    # Read data from the file as str
    with open(file_dir, encoding=\"utf-8\") as f:
        messages = f.read()

我尝试使用 heroku bash 命令检查文件系统结构并确认应用程序目录,但命令执行并返回空白。 我的文件组织如下:

MyProject
    > static
        > encouragement.txt
    app.py

不确定是否需要,但我正在使用 Windows。谢谢你。

更新: 我尝试添加 index.php 和 composer.json 作为几个教程的建议。它们不适用于 txt 文件;不确定他们是否对其他静态文件有所帮助。不是最好的解决方案,但我最终只是将所有 txt 内容复制并粘贴到我的 app.py 中。

    标签: flask heroku deployment


    【解决方案1】:

    在对我的环境变量/配置进行了一些“清理”后,我今天遇到了这个问题。问题是您试图打开一个目录,就好像它是一个文件一样,这会导致错误。我推荐this article中的答案(如图)。

    import os
    
    # path to your file
    file_name = r'/app/static/encouragement.txt'
    
    print(os.path.isfile(file_name))
    
    with open(file_name, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
        print(lines)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2018-06-20
      • 2022-12-07
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多