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.
Result
Returns
The underlying raw binary stream buffer.
Run it
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())
Output
Press Run to see what it prints.Worth knowing
- Text/Binary boundary: Detach is used in low-level I/O libraries to take ownership of the underlying raw byte buffer.