FileNotFoundError 错误码 2 没有这样的文件或目录

huangapple go评论55阅读模式
英文:

filenotfounderror errno 2 no such file or directory

问题

我正在尝试使用discord.py编写一个Discord机器人。

在我的机器人中,我有一个命令,它获取一张图片,然后在压缩并发送它之后执行某些操作

我的所有代码都正常,但在发送zip文件时出现了错误

这是错误文本

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Hesam\\Desktop\\New folder\\export_bdce64fb-079e-4b51-b6f0-8f798475aba6.zip'

这是我的命令函数:

@bot.command()
async def totem(ctx):
    try:
        url = ctx.message.attachments[0].url
    except IndexError:
        print("Error: No attachments")
        await ctx.send("No attachments detected!")
    else:
        if url.startswith("https://cdn.discordapp.com"):
            r = requests.get(url, stream=True)
            if r.status_code == 200:
                now = datetime.datetime.now()
                year = now.year
                month = now.month
                day = now.day
                hour = now.hour
                minute = now.minute
                second = now.second
                destination_path = os.path.join("exports", f"{year}", f"{month}", f"{day}", f"{hour}", f"{minute}", f"{second}")
                src = os.path.join(os.getcwd(), "src")
                print(src)
                os.makedirs(destination_path, exist_ok=True)
                if os.path.exists(destination_path):
                    shutil.rmtree(destination_path)
                shutil.copytree(src, destination_path)
                file_name = os.path.join(destination_path, "assets", "minecraft", "textures", "item", "totem_of_undying.png")
                await ctx.send("making resource pack...")
                with open(file_name, 'wb') as f:
                    for chunk in r.iter_content(1024):
                        f.write(chunk)
                unicId = uuid.uuid4()
                zip_name = os.path.join(os.getcwd(), f"export_{unicId}.zip")
                print(zip_name)
                shutil.make_archive(zip_name, 'zip', destination_path)

                # file=discord.File(zip_name)
                await ctx.send(file=discord.File(zip_name))

            else:
                await ctx.send("Error: Failed to fetch the image!")
        else:
            await ctx.send("Invalid image link!")

注意: 请注意在代码中进行了一些修复,以解决错误。如果您需要进一步的帮助,请告诉我。

英文:

I'm trying to write a discord bot using discord.py.

In my bot I have command that get a image and do something after that zip it and send it.

All of my code is OK but I have a error in sending zip file.

This is error text:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Hesam\\Desktop\\New folder\\export_bdce64fb-079e-4b51-b6f0-8f798475aba6.zip'

This is function of my command:

@bot.command()
async def totem(ctx):
try:
url = ctx.message.attachments[0].url
except IndexError:
print("Error: No attachments")
await ctx.send("No attachments detected!")
else:
if url.startswith("https://cdn.discordapp.com"):
r = requests.get(url, stream=True)
if r.status_code == 200:
now = datetime.datetime.now()
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
destination_path = os.path.join("exports", f"{year}", f"{month}", f"{day}", f"{hour}", f"{minute}", f"{second}")
src = os.path.join(os.getcwd(), "src")
print(src)
os.makedirs(destination_path, exist_ok=True)
if os.path.exists(destination_path):
shutil.rmtree(destination_path)
shutil.copytree(src, destination_path)
file_name = os.path.join(destination_path, "assets", "minecraft", "textures", "item", "totem_of_undying.png")
await ctx.send("making reasourse pack...")
with open(file_name, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
unicId = uuid.uuid4()
zip_name = os.path.join(os.getcwd(), f"export_{unicId}.zip")
print(zip_name)
shutil.make_archive(zip_name, 'zip', destination_path)
# file=discord.File(zip_name)
await ctx.send(file=discord.File(zip_name))
else:
await ctx.send("Error: Failed to fetch the image!")
else:
await ctx.send("Invalid image link!")

答案1

得分: 1

文档中:

base_name是要创建的文件名,包括路径,但不包括任何特定格式的扩展名。

所以不要在存档名称中包括.zip。否则,您会寻找f"export_{unicId}.zip.zip"。请改为以下方式:

zip_name = os.path.join(os.getcwd(), f"export_{unicId}")
shutil.make_archive(zip_name, 'zip', destination_path)
英文:

From the docs:

> base_name is the name of the file to create, including the path, minus any format-specific extension.

So do not include .zip in your archive name. Otherwise you are looking for f"export_{unicId}.zip.zip". Do this instead:

zip_name = os.path.join(os.getcwd(), f"export_{unicId}")
shutil.make_archive(zip_name, 'zip', destination_path)

</details>



huangapple
  • 本文由 发表于 2023年7月18日 03:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定