找不到文件,但该文件确实存在,给出了完整路径等

2024-06-02 11:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的密码:

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'

希望你能帮忙


Tags: inidaswithlinestorageopenme
2条回答

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

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

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

相关问题 更多 >