【问题标题】:File not found, but the file really does exist, full path given, etc未找到文件,但文件确实存在,给出了完整路径等
【发布时间】:2020-12-29 23:09:30
【问题描述】:

文件确实存在,路径正确。我敢肯定。你们知道怎么回事吗?

这是我的代码:

with open(f'C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\storage\{user.id}Istore.txt', 'r') as f:
    lines = f.readlines()
    title = lines[0]
    desc = lines[1]
    embed=discord.Embed(color=0x2f3136, description=desc, title=title)
    with open(f'C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\storage\{ctx.author.id}Istoreitems.txt', 'r') as f:
        lines = f.readlines()
        for line in lines:
            line = line.replace("\n", '')
            with open(f'C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\storage\{ctx.author.id}Istore{line}', 'r') as f:
                lines = f.readlines()
                name = lines[0]
                price = lines[2]
                with open(f'C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\storage\{channel.guild.id}.txt', 'r') as f:
                    lines = f.readlines()
                    currencyname = lines[1]
                    embed.add_field(title=name, value=price + " " + currencyname)
await ctx.send(embed=embed)

追溯:

Traceback (most recent call last):
  File "C:\Users\jakec\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\sf.py", line 221, in scmd
    with open(os.path.join(f'C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\storage\{ctx.author.id}Istore', line), 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\(A) Me\\Coding\\Python\\jcjakec\\Bots\\storefront\\storage\\564054910086283286Istore\\test'

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

Traceback (most recent call last):
  File "C:\Users\jakec\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\jakec\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\jakec\AppData\Local\Programs\Python\Python38-32\lib\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: 'C:\\(A) Me\\Coding\\Python\\jcjakec\\Bots\\storefront\\storage\\564054910086283286Istore\\test'

希望你能帮忙!

【问题讨论】:

  • 您发布的代码的哪一行是第 221 行,因为我在您提出的问题中看不到该代码。
  • @barny 它是一个不和谐的机器人 - 正在进行中,因此该文件远远超出了此范围。我相信其中一条是第 221 行。
  • 确定这个文件最后没有.txt就可以了吗?
  • @AlexanderGolys 我的天,我是个白痴。那解决了!很抱歉浪费大家的时间 - 愚蠢的错误。

标签: python python-3.x file text-files discord.py


【解决方案1】:

您需要通过将反斜杠 (\) 加倍 (\\) 来转义它们。否则它们将被解释为特殊字符。

【讨论】:

  • 没关系,这是我犯的一个愚蠢的错误 - 忘记了.txt 抱歉!
【解决方案2】:

更简单的方法是在文件前面放一个 r。 前 -

with open(r'C:\(A) Me\Coding\Python\jcjakec\Bots\storefront\storage\{user.id}Istore.txt', 'r') as f:

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2014-06-28
    • 2014-12-21
    相关资源
    最近更新 更多