【问题标题】:Why can't heroku get the text files on my folder while running my program?为什么 heroku 在运行我的程序时无法获取我文件夹中的文本文件?
【发布时间】:2022-01-14 15:33:48
【问题描述】:

我正在用 python 做一个简单的不和谐机器人,我正在使用 heroku 来托管它。我有一些返回大文本的命令,所以我将这些文本放在单独的 .txt 文件中,并将所有这些文件组织在一个名为“文件”的文件夹中。然后我使用代码访问和读取这些文件。当我托管机器人时一切正常,但 heroku 在托管时总是返回“FileNotFoundError”。

完整的错误信息:

Ignoring exception in command helpie:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.10/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/app/main.py", line 27, in helpie
helpmessage = open(".\files\helpMessage.txt", "r", encoding = "utf-8")
FileNotFoundError: [Errno 2] No such file or directory: '.\x0ciles\\helpMessage.txt'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/app/.heroku/python/lib/python3.10/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.10/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: FileNotFoundError: [Errno 2] No such file or directory: '.\x0ciles\\helpMessage.txt'

这是使用 .txt 文件的命令之一。此代码位于 heroku 正在读取的 main.py 文件中。

@bot.command()
async def helpie(ctx):
    helpmessage = open(".\files\helpMessage.txt", "r", encoding = "utf-8")
    await ctx.send(helpmessage.read())
    helpmessage.close()

这是程序根文件夹及其所有文件的树:

C:.
│   .gitignore
│   main.py
│   Procfile
│   README.md
│   requirements.txt
│   runtime.txt
│
├───.vscode
└───files
        bot icon.png
        geraldoMessage.txt
        helpMessage.txt

不知道有没有必要,不过我也会把“requirements.txt”和“Procfile”的内容包括进去。

过程文件:

worker: python main.py

requirements.txt

discord.py
asyncio

感谢您的帮助。

【问题讨论】:

  • 您是否尝试过使用 heroku 中文件的绝对路径来提供给open() 语句?此外,如果 heroku 使用 / 作为路径分隔符,您尝试访问的文件可能具有不同的路径。
  • 我不会使用绝对路径,因为这几乎肯定会在环境之间有所不同(特别是在 Heroku 和似乎是本地 Windows 机器之间)。但我建议使路径相对于已知位置。现在它与工作目录相关。
  • 完整的错误信息是什么?我怀疑你得到了类似No such file or directory: '.\x0ciles\\helpMessage.txt'的东西?

标签: python file heroku discord discord.py


【解决方案1】:

我怀疑更完整的错误是这样的:

No such file or directory: '.\x0ciles\\helpMessage.txt'

直接的问题可能是您在字符串中包含 \f 时不小心使用了 escape sequence,这表示 ASCII 表单提要。

请注意,路径已损坏。您可以在这里使用原始字符串:

open(r".\files\helpMessage.txt", "r", encoding = "utf-8")
#    ^ raw string

或者您可以更改定义该路径的方式。正斜杠比反斜杠更安全,使用pathlibos.path 将各个路径组件连接在一起更安全。

我认为这不是您的直接问题,但您的路径也与工作目录的位置有关。我建议将其相对于已知位置,例如项目的根目录。

把它们放在一起,试试这样的东西(使用pathlib):

from pathlib import Path

# This will point to the right directory no matter what the working directory is
root_path = Path(__file__).resolve().parent


@bot.command()
async def helpie(ctx):
    helpmessage = open(root_path / "files" / "helpMessage.txt", "r", encoding = "utf-8")
    #                            ^         ^ safely joining components
    #                  ^^^^^^^^^ known path
    await ctx.send(helpmessage.read())
    helpmessage.close()

或像这样(使用os.path):

import os.path

root_path = os.path.dirname(os.path.abspath(__file__))


@bot.command()
async def helpie(ctx):
    helpmessage = open(os.path.join(root_path, "files", "helpMessage.txt"), "r", encoding = "utf-8")
    #                               ^^^^^^^^^ known path
    #                  ^^^^^^^^^^^^ safely joining components
    await ctx.send(helpmessage.read())
    helpmessage.close()

【讨论】:

    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2011-04-14
    相关资源
    最近更新 更多