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

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

filenotfounderror errno 2 no such file or directory

问题

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

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

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

这是错误文本

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

这是我的命令函数:

  1. @bot.command()
  2. async def totem(ctx):
  3. try:
  4. url = ctx.message.attachments[0].url
  5. except IndexError:
  6. print("Error: No attachments")
  7. await ctx.send("No attachments detected!")
  8. else:
  9. if url.startswith("https://cdn.discordapp.com"):
  10. r = requests.get(url, stream=True)
  11. if r.status_code == 200:
  12. now = datetime.datetime.now()
  13. year = now.year
  14. month = now.month
  15. day = now.day
  16. hour = now.hour
  17. minute = now.minute
  18. second = now.second
  19. destination_path = os.path.join("exports", f"{year}", f"{month}", f"{day}", f"{hour}", f"{minute}", f"{second}")
  20. src = os.path.join(os.getcwd(), "src")
  21. print(src)
  22. os.makedirs(destination_path, exist_ok=True)
  23. if os.path.exists(destination_path):
  24. shutil.rmtree(destination_path)
  25. shutil.copytree(src, destination_path)
  26. file_name = os.path.join(destination_path, "assets", "minecraft", "textures", "item", "totem_of_undying.png")
  27. await ctx.send("making resource pack...")
  28. with open(file_name, 'wb') as f:
  29. for chunk in r.iter_content(1024):
  30. f.write(chunk)
  31. unicId = uuid.uuid4()
  32. zip_name = os.path.join(os.getcwd(), f"export_{unicId}.zip")
  33. print(zip_name)
  34. shutil.make_archive(zip_name, 'zip', destination_path)
  35. # file=discord.File(zip_name)
  36. await ctx.send(file=discord.File(zip_name))
  37. else:
  38. await ctx.send("Error: Failed to fetch the image!")
  39. else:
  40. 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:

  1. 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:

  1. @bot.command()
  2. async def totem(ctx):
  3. try:
  4. url = ctx.message.attachments[0].url
  5. except IndexError:
  6. print("Error: No attachments")
  7. await ctx.send("No attachments detected!")
  8. else:
  9. if url.startswith("https://cdn.discordapp.com"):
  10. r = requests.get(url, stream=True)
  11. if r.status_code == 200:
  12. now = datetime.datetime.now()
  13. year = now.year
  14. month = now.month
  15. day = now.day
  16. hour = now.hour
  17. minute = now.minute
  18. second = now.second
  19. destination_path = os.path.join("exports", f"{year}", f"{month}", f"{day}", f"{hour}", f"{minute}", f"{second}")
  20. src = os.path.join(os.getcwd(), "src")
  21. print(src)
  22. os.makedirs(destination_path, exist_ok=True)
  23. if os.path.exists(destination_path):
  24. shutil.rmtree(destination_path)
  25. shutil.copytree(src, destination_path)
  26. file_name = os.path.join(destination_path, "assets", "minecraft", "textures", "item", "totem_of_undying.png")
  27. await ctx.send("making reasourse pack...")
  28. with open(file_name, 'wb') as f:
  29. for chunk in r.iter_content(1024):
  30. f.write(chunk)
  31. unicId = uuid.uuid4()
  32. zip_name = os.path.join(os.getcwd(), f"export_{unicId}.zip")
  33. print(zip_name)
  34. shutil.make_archive(zip_name, 'zip', destination_path)
  35. # file=discord.File(zip_name)
  36. await ctx.send(file=discord.File(zip_name))
  37. else:
  38. await ctx.send("Error: Failed to fetch the image!")
  39. else:
  40. await ctx.send("Invalid image link!")

答案1

得分: 1

文档中:

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

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

  1. zip_name = os.path.join(os.getcwd(), f"export_{unicId}")
  2. 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:

  1. zip_name = os.path.join(os.getcwd(), f"export_{unicId}")
  2. shutil.make_archive(zip_name, 'zip', destination_path)
  3. </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:

确定