Skip to content
File Methods

detach()

Separate the underlying binary buffer from the text stream and return it.

file.detach()

file.detach() separates the underlying raw binary stream buffer from the text stream layer and returns the buffer.

Once detached, the original text stream is in an unusable state.

Returns

The underlying raw binary stream buffer.

Try it

Run it and change it
import io
buffer = io.BytesIO(b'hello raw')
wrapper = io.TextIOWrapper(buffer, encoding='utf-8')
raw = wrapper.detach()
print(raw.getvalue())

Worth knowing

  • Text/Binary boundary: Detach is used in low-level I/O libraries to take ownership of the underlying raw byte buffer.

Related entries