简体   繁体   English

我收到一条错误消息,指出 AttributeError: 'str' object has no attribute 'read',我不知道如何修复它

[英]I'm getting an error that says AttributeError: 'str' object has no attribute 'read' and i do not know how to fix it

    @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)

This is the code i'm using and the error is AttributeError: 'str' object has no attribute 'read' Please tell me how i can fix this i would appreciate it this is the traceback btw:这是我正在使用的代码,错误是 AttributeError: 'str' object has no attribute 'read' 请告诉我如何解决这个问题,我很感激这是回溯 btw:

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

It says NoneType can't be used in await expression but i think the main problem is the str thing but im not sure它说 NoneType 不能用于 await 表达式,但我认为主要问题是 str 的事情,但我不确定

link2 = BytesIO(link)
image = await link2.read()

You should check the BytesIO(link) return value, as it seems to be a string.您应该检查 BytesIO(link) 返回值,因为它似乎是一个字符串。 Which has no "read" method.其中没有“阅读”方法。

What are you trying to do with your caode ?你想用你的 caode 做什么?

According to the doc: https://docs.python.org/3/library/io.html根据文档: 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.

It looks like you give a string argument to the BytesIO(link) constructor.看起来您为 BytesIO(link) 构造函数提供了一个字符串参数。 link seems to be a string ?链接似乎是一个字符串? But it needs some bytes, not a string.但它需要一些字节,而不是字符串。

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

So you have to change the argument type, or maybee use StringIO instead.所以你必须改变参数类型,或者使用 StringIO 代替。 I let you chose and try this.我让你选择并尝试这个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 python 中收到此错误:“AttributeError: 'str' object has no attribute 'set'”。 我正在使用 tkinter,我该如何解决这个问题? - I'm getting this error in python: “AttributeError: 'str' object has no attribute 'set'”. I'm using tkinter, how do I fix this? 我收到 discord.py 错误,说 AttributeError: 'str' object has no attribute 'read' - I'm getting a discord.py error saying AttributeError: 'str' object has no attribute 'read' 我收到一个错误:AttributeError: &#39;str&#39; object has no attribute &#39;isfloat&#39; - I am getting an error : AttributeError: 'str' object has no attribute 'isfloat' 如何修复错误 AttributeError: 'function' object has no attribute 'str' - How can I fix the error AttributeError: 'function' object has no attribute 'str' AttributeError: 'function' object 没有属性 'read' — 我该如何修复? - AttributeError: 'function' object has no attribute 'read' — HOW CAN I FIX? 如何修复“AttributeError: &#39;str&#39; object has no attribute &#39;content&#39;”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error 如何修复错误:AttributeError: 'str' object has no attribute 'text' - How to fix error: AttributeError: 'str' object has no attribute 'text' 我得到了AttributeError:当我在Windows上运行我的程序时,&#39;module&#39;对象没有属性&#39;fork&#39;。 我怎样才能解决这个问题? - I'm getting AttributeError: 'module' object has no attribute 'fork' when i run my program on windows. How can i fix this? 如何修复 AttributeError: &#39;bytes&#39; object has no attribute &#39;encode&#39;? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? 如何修复 AttributeError: 'NoneType' object 没有属性 'lower'? - How do i fix AttributeError: 'NoneType' object has no attribute 'lower'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM