简体   繁体   English

为什么 heroku 在运行我的程序时无法获取我文件夹中的文本文件?

[英]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.正斜杠比反斜杠更安全,使用pathlibos.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.

相关问题 为什么我的按钮在程序运行时不起作用? - Why doesn't my button work while program is running? 为什么我不能只将文本文件移动到Python中的其他文件夹? - Why can't I move just my text files to different folder in Python? 为什么程序中的while循环运行到无穷大 - Why is the while loop in my program running to infinity 为什么我的程序不能将整数列表转换为文本(字符串)? - why can't my program convert a list of integars to text (string)? 为什么我的测试文件夹中的测试文件无法从源文件夹中导入源文件? - Why my test files from a test folder can't import the source files from a source folder? 为什么 Heroku 不喜欢我的文件夹结构? - Why doesn't Heroku like my folder structure? 为什么我无法使用os.listdir更改Python程序中文件的名称 - Why I can't change the name of my files in my Python program using os.listdir 为什么我在运行 class 程序时收到此 NameError? - Why I am getting this NameError while running my class program? 我无法让 AWS S3 在我的 Django/Heroku 应用程序上提供媒体文件 - I can't get AWS S3 to serve media files on my Django/Heroku App 我不明白为什么在运行程序时输出中没有显示“ None” - I don't understand why I get a 'None' in my ouput when running the program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM