[英]Why can't heroku get the text files on my folder while running my program?
I am doing a simple discord bot with python and I'm using heroku to host it.我正在使用 python 做一个简单的 discord 机器人,我正在使用 heroku 来托管它。 I have some commands that return a big text, so I put these texts on separate.txt files and organized all those files in a single folder called "files".我有一些返回大文本的命令,所以我将这些文本放在单独的.txt 文件中,并将所有这些文件组织在一个名为“文件”的文件夹中。 Then I used the code to access and read those files.然后我使用代码访问和读取这些文件。 Everything works fine when I am hosting the bot, but heroku always returns a "FileNotFoundError" when he is hosting.当我托管机器人时一切正常,但 heroku 在托管时总是返回“FileNotFoundError”。
Here is one of the commands that use a.txt file.这是使用 a.txt 文件的命令之一。 This code is on the main.py file, that heroku is reading.此代码位于 main.py 文件中,heroku 正在读取该文件。
@bot.command()
async def helpie(ctx):
helpmessage = open(".\files\helpMessage.txt", "r", encoding = "utf-8")
await ctx.send(helpmessage.read())
helpmessage.close()
Here is the tree of the root folder of the program and all it's files:这是程序根文件夹的树及其所有文件:
C:.
│ .gitignore
│ main.py
│ Procfile
│ README.md
│ requirements.txt
│ runtime.txt
│
├───.vscode
└───files
bot icon.png
geraldoMessage.txt
helpMessage.txt
I don't know if it is necessary, but I will also include the content of the "requirements.txt" and "Procfile".不知道有没有必要,不过我也会把“requirements.txt”和“Procfile”的内容包括进去。
Procfile:档案:
worker: python main.py
requirements.txt要求.txt
discord.py
asyncio
Thank you for the help.感谢您的帮助。
I suspect a more complete error would be something like this:我怀疑一个更完整的错误是这样的:
No such file or directory: '.\x0ciles\\helpMessage.txt'
The immediate issue is probably that you're accidentally using an escape sequence by including \f
in your string, which indicates an ASCII form feed.直接的问题可能是您在字符串中包含\f
时不小心使用了转义序列,这表示 ASCII 换页符。
Note that the path has been mangled.请注意,路径已被破坏。 You could us a raw string here:您可以在这里使用原始字符串:
open(r".\files\helpMessage.txt", "r", encoding = "utf-8")
# ^ raw string
Or you could change how you define that path.或者您可以更改定义该路径的方式。 Forward slashes are safer than backslashes, and joining individual path components together using pathlib
or os.path
is safer still.正斜杠比反斜杠更安全,使用pathlib
或os.path
将各个路径组件连接在一起更安全。
I don't think this is your immediate issue, but your path is also relative to whatever the working directory happens to be.我认为这不是您的直接问题,但您的路径也与工作目录有关。 I suggest making it relative to a known location, eg the root directory of your project.我建议将其设置为相对于已知位置,例如项目的根目录。
Putting that together, try something like this (using pathlib
):把它们放在一起,尝试这样的事情(使用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()
or like this, using os.path
:或者像这样,使用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()
Note that in both cases I changed the backslashed string to individual arguments for each path component.请注意,在这两种情况下,我将每个路径组件的反斜杠字符串更改为单独的 arguments。 Backslashes in strings are tricky, and in this particular case `字符串中的反斜杠很棘手,在这种特殊情况下 `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.