[Python] How to write StreamReader type to an image file in python?

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    url=[...]

    async with aiohttp.ClientSession(headers=headerschrome) as session:
    task = asyncio.create_task(fetch(session, url))

    async def fetch(s, url):
    async with s.request(method='GET',url=url) as r:
    if r.status != 200:
    r.raise_for_status()
    with open('zzz/a.webp','wb') as xd:
    xd.write(r.content)


    xd.write(r.content)
    TypeError: a bytes-like object is required, not 'StreamReader'


    I receive this error when writing the response content to a file. Trying to convert StreamReader to bytes also fail with


    bytes(r.content)
    ​

    Also is there a way to append image content to a single file? Thanks a lot!

    Continue reading...

Compartilhe esta Página