Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

io.BufferedIOBase

A classe base descreve os fluxos binários com memória intermédia.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

io.BufferedIOBase()

A classe base descreve os fluxos binários com memória intermédia.

O fecho é herdado de IOBase. O fluxo concreto implementa as operações que suporta entre read, read1, write e detach. Os métodos fornecidos readinto e readinto1 preenchem a memória intermédia gravável recebida e devolvem o número de bytes escritos. readable() e writable() indicam as operações disponíveis. Para b'abc', chamadas sucessivas a read(2) num BufferedReader devolvem b'ab', b'c' e b''. O último bloco pode ser mais curto. read(0) devolve b'' mesmo antes do fim.

Escrever neste leitor lança io.UnsupportedOperation.

import io
with io.BufferedReader(io.BytesIO(b'xy')) as reader:
    assert reader.read(0)==b'' and reader.read(1)==b'x'
    assert reader.read(10)==b'y' and reader.read(1)==b''
    try: reader.write(b'z')
    except io.UnsupportedOperation: pass
    else: raise AssertionError
with io.BufferedWriter(io.BytesIO()) as writer:
    assert isinstance(writer, io.BufferedIOBase) and writer.write(b'x')==1
with io.BufferedReader(io.BytesIO(b'xyz')) as reader:
    target=bytearray(2)
    assert reader.readinto(target)==2 and target==b'xy'
    assert reader.readinto1(target)==1 and target==b'zy'
    assert reader.readinto(target)==0

Exemplos

import io
with io.BufferedReader(io.BytesIO(b"abc")) as stream:
    print(isinstance(stream, io.BufferedIOBase))
    print(stream.read(2), stream.read(2), stream.read(2))

Resultado

True
b'ab' b'c' b''

Escrever neste leitor lança io.UnsupportedOperation.

Fontes