【问题标题】:I'm getting an error that says AttributeError: 'str' object has no attribute 'read' and i do not know how to fix it我收到一条错误消息,显示 AttributeError: 'str' object has no attribute 'read' 我不知道如何修复它
【发布时间】:2021-03-05 14:52:41
【问题描述】:
    @commands.command()
    async def trash(self, ctx, user: discord.Member):
        auth = str(os.getenv("FLIPNOTE_API"))
        url2 = f'https://api.alexflipnote.dev/trash?face={ctx.author.avatar_url_as(format="png")}&trash={user.avatar_url_as(format="png")}'
        headers = {'Authorization': auth}
        async with aiohttp.ClientSession() as session:
            async with session.get(url2, headers=headers) as res:
                link = re.compile(r"(?<=<ClientResponse\()(.*)(?=\))").search(repr(res)).group(1)
                link2 = BytesIO(link)
                image = await link2.read()
                file = discord.File(image, filename="lol.png")
                await ctx.send(file=file)

这是我正在使用的代码,错误是 AttributeError: 'str' object has no attribute 'read' 请告诉我如何解决这个问题,我将不胜感激 这是追溯顺便说一句:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/runner/DayStride/cogs/fun.py", line 1461, in trashmoment
    link2 = BytesIO(link)
TypeError: a bytes-like object is required, not 'str'

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

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/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: TypeError: a bytes-like object is required, not 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 71, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/runner/DayStride/cogs/fun.py", line 1480, in trasherror
    await await ctx.send(error)
TypeError: object NoneType can't be used in 'await' expression

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

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 943, in on_message
    await self.process_commands(message)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 940, in process_commands
    await self.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 907, in invoke
    await ctx.command.dispatch_error(ctx, exc)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 422, in dispatch_error
    await injected(cog, ctx, error)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 77, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object NoneType can't be used in 'await' expression

它说 NoneType 不能用于 await 表达式,但我认为主要问题是 str 的东西,但我不确定

【问题讨论】:

  • 请发布回溯消息,以便我们在上下文中查看完整消息和失败行。
  • 顺便说一句,re 保留了最近使用的正则表达式的缓存。如果你这样做link = re.search(r"(?&lt;=&lt;ClientResponse\()(.*)(?=\))", repr(res)).group(1),它可能比每次都重新编译更有效。
  • 我已经添加回溯
  • 您的帖子标题显示“我收到一条错误消息,提示 AttributeError: 'str' object has no attribute 'read'”,但我在您发布的回溯中的任何地方都没有看到。
  • 好的,告诉我你打算如何让代码工作。你做一些正则表达式的事情并从用户的消息中得到一个字符串,你将它保存为link。然后你想尝试从那个字符串创建一个BytesIO 对象......为什么?然后你想从那个流中读取......这应该以某种方式创建一个image?怎么样?

标签: python discord.py


【解决方案1】:
link2 = BytesIO(link)
image = await link2.read()

您应该检查 BytesIO(link) 返回值, 因为它似乎是一个字符串。 没有“读取”方法。

你想用你的 caode 做什么?

【讨论】:

  • 不一定。诸如discord.File 之类的调用函数可能会引发错误。
  • 您可能是对的,只是 OP 需要在我们知道之前提供更多上下文。
  • 我基本上在使用一个 api 你可以看到它here 并且 res 发送的不是我想要的我想要它做的我想要端点的图片但我正在努力位
  • 顺便说一句,我实际上看不到 BytesIO(link) 的返回值,因为它返回错误
【解决方案2】:

根据文档: https://docs.python.org/3/library/io.html

In-memory streams
It is also possible to use a str or bytes-like object as a file for
both reading     and writing. For strings StringIO can be used like a
file opened in text mode.
BytesIO can be used like a file opened in binary mode. Both provide full
read-write capabilities with random access.


Buffered Streams
Buffered I/O streams provide a higher-level interface to an I/O device
than raw I/O does.

class io.BytesIO([initial_bytes])
A binary stream using an in-memory bytes buffer. It inherits BufferedIOBase.
The buffer is discarded when the close() method is called.

The optional argument initial_bytes is a bytes-like object that contains
initial data.

看起来你给 BytesIO(link) 构造函数提供了一个字符串参数。链接似乎是一个字符串?但它需要一些字节,而不是字符串。

>>> view = b.getbuffer()
>>> view[2:4] = b"56"
>>> b.getvalue()

所以你必须改变参数类型,或者用 StringIO 代替。 我让你选择并尝试一下。

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多