io.BytesIO
Prev: io.BufferedWriter | Up: io | Next: io.FileIO
Rel: io.BlockingIOError, io.BufferedIOBase, io.BufferedRandom, io.BufferedReader
Ref: docs.python.org — io.BytesIO
Tags: #public #python3 #stdlib #io
Import
import io
from io import BytesIO
Signature & Overview
io.BytesIO(initial_bytes=b'')
Buffered I/O implementation using an in-memory bytes buffer.
Methods & Attributes
BytesIO.close(self, /)— Disable all I/O operations.BytesIO.detach(self, /)— Disconnect this buffer from its underlying raw stream and return it.BytesIO.fileno(self, /)— Return underlying file descriptor if one exists.BytesIO.flush(self, /)— Does nothing.BytesIO.getbuffer(self, /)— Get a read-write view over the contents of the BytesIO object.BytesIO.getvalue(self, /)— Retrieve the entire contents of the BytesIO object.BytesIO.isatty(self, /)— Always returns False.BytesIO.read(self, size=-1, /)— Read at most size bytes, returned as a bytes object.BytesIO.read1(self, size=-1, /)— Read at most size bytes, returned as a bytes object.BytesIO.readable(self, /)— Returns True if the IO object can be read.BytesIO.readinto(self, buffer, /)— Read bytes into buffer.BytesIO.readinto1(self, buffer, /)—BytesIO.readinto1method/property.BytesIO.readline(self, size=-1, /)— Next line from the file, as a bytes object.BytesIO.readlines(self, size=None, /)— List of bytes objects, each a line from the file.BytesIO.seek(self, pos, whence=0, /)— Change stream position.BytesIO.seekable(self, /)— Returns True if the IO object can be seeked.BytesIO.tell(self, /)— Current file position, an integer.BytesIO.truncate(self, size=None, /)— Truncate the file to at most size bytes.BytesIO.writable(self, /)— Returns True if the IO object can be written.BytesIO.write(self, b, /)— Write bytes to file.BytesIO.writelines(self, lines, /)— Write lines to the file.
Usage
import io
buf = io.BytesIO()
buf.write(b"hello ")
buf.write(b"bytes")
assert buf.getvalue() == b"hello bytes"
buf.seek(0)
assert buf.read(5) == b"hello"
Gotchas
- Remember
buf.seek(0)before.read(): After.write(), the cursor sits at the end so.read()returnsb""unless you rewind (or use.getvalue()). getbuffer()avoids copying: Returns a zero-copymemoryview, though theBytesIOcannot be resized until the view is released.
REPL Inspect
from io import BytesIO
help(BytesIO)
Linked from
Related notes
- tuts/python3/stdlib_module/io/BufferedWriter Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/FileIO Linked here; links to this page; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/io/BlockingIOError Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedIOBase Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedRandom Linked here; shared tags: #io, #stdlib
- tuts/python3/stdlib_module/io/BufferedReader Linked here; shared tags: #io, #stdlib
- tuts/c/stdlib/stdio_and_file_io shared tags: #io, #stdlib
- tuts/cpp/stdlib/io_and_filesystem shared tags: #io, #stdlib
- tuts/cpp/stdlib/io_and_filesystem/format_and_print shared tags: #io, #stdlib