Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

io.bytesio

import io.bytesio

io lifts part of this module out to its own top level; each name below is shown with the path that reaches it. Anything still spelled io.bytesio.* needs import io.bytesio.

BytesIO: an in-memory buffer that behaves like a file.

Anything that reads or writes through a file handle can be pointed at one of these instead, which is what makes a function that writes to disk testable without touching the disk.

Classes

BytesIO

class io.BytesIO

The BytesIO class implements a bytearray based I/O system that allows you use treat bytearray (bytes) as if they were a file.

The class implements the essentials of a file except those that ties it to the operating system filesystem such as symbolic links, chmod and set time.

See the chapter on files in The Zuri Programming Language for more information.

Constructor

io.BytesIO(source, mode)

Returns a new instance of BytesIO

Parameters

  • source (bytes)
  • mode (string) — The I/O open mode - Default is r

BytesIO.exists()

io.BytesIO.exists() -> bool

Returns true as BytesIO always exist.

Returns bool

BytesIO.close()

io.BytesIO.close()

Closes the stream to an opened BytesIO. You’ll rarely ever need to call this method yourself in most use cases.

BytesIO.open()

io.BytesIO.open() -> bool

Opens the stream to a BytesIO for the operation originally specified on the BytesIO object during creation.

You may need to call this method after a call to read() if the length isn’t specified or write() if you wish to read or write again as the BytesIO will already be closed.

Returns bool

BytesIO.read()

io.BytesIO.read(length) -> bytes

Reads the content of an opened BytesIO up to the specified length and returns it as string or bytes if the BytesIO was opened in the binary mode. If the length is not specified, the BytesIO will be read to the end.

This method requires that the BytesIO be opened in the read mode (default mode) or a mode that supports reading. If you aren’t reading the full length of the BytesIO, you’ll need to call the close() method to free the BytesIO for further reading, otherwise, the close() method will be automatically called for you.

Parameters

  • length (number) — Default = -1

Returns bytes

Raises Error

BytesIO.gets()

io.BytesIO.gets(length) -> bytes

Same as read(), but doesn’t open or close the BytesIO automatically.

Parameters

  • length (number) — Default = -1

Returns bytes

Raises Error

BytesIO.write()

io.BytesIO.write(data) -> number

Writes a string or bytes to an opened BytesIO at the current insertion point. When the BytesIO is opened with the a mode enabled, write will always start from the end of the BytesIO.

If the seek() method has been previously called, write will begin from the seeked position, otherwise it will start at the beginning of the BytesIO.

Parameters

  • `` (bytes|string)

Returns number

BytesIO.puts()

io.BytesIO.puts(data) -> number

Same as write(), but doesn’t open or close the BytesIO automatically.

Parameters

  • `` (bytes|string)

Returns number

BytesIO.number()

io.BytesIO.number() -> number

Returns the integer file descriptor number that is used by the underlying implementation to request I/O operations from the operating system. This can be very useful for low-level interfaces that uses or act as BytesIO descriptors.

Returns number

BytesIO.is_tty()

io.BytesIO.is_tty() -> bool

Always returns false as a BytesIO is not a TTY device.

Returns bool

BytesIO.is_open()

io.BytesIO.is_open() -> bool

Returns true if the BytesIO is open for reading or writing and false otherwise.

Returns bool

BytesIO.is_closed()

io.BytesIO.is_closed()

Returns true if the BytesIO is closed for reading or writing and false otherwise.

BytesIO.flush()

io.BytesIO.flush()

Does nothing for a BytesIO

io.BytesIO.symlink(path) -> bool

Does nothing for BytesIO but simply returns false because BytesIO cannot be symbolically linked.

Returns bool

BytesIO.stats()

io.BytesIO.stats() -> dict

Returns the statistics or details of the BytesIO.

See the working with files documentation for more information about the stats() method.

Returns dict

BytesIO.delete()

io.BytesIO.delete() -> bool

Clears the bytearray and closes it for reading or writing.

Any further attempt to perform most operations on the BytesIO after calling delete() will raise an error.

Returns bool

BytesIO.rename()

io.BytesIO.rename(new_name) -> bool

Returns false because BytesIO cannot be renamed.

Returns bool

BytesIO.copy()

io.BytesIO.copy() -> [[io.BytesIO]]

Returns a new BytesIO with the source cloned and opened with the same mode as the current BytesIO.

Returns [[io.BytesIO]]

BytesIO.path()

io.BytesIO.path() -> string

Returns an empty string because BytesIO do not have any physical path.

Returns string

BytesIO.abs_path()

io.BytesIO.abs_path() -> string

Same as [[io.BytesIO.path()]].

Returns string

BytesIO.truncate()

io.BytesIO.truncate(length) -> bool

Truncates the entire BytesIO if length is not given or truncates the BytesIO such that only length number of bytes is left in it.

Returns bool

BytesIO.chmod()

io.BytesIO.chmod(number) -> bool

Returns false because BytesIO do not have a permission scheme.

Returns bool

BytesIO.set_times()

io.BytesIO.set_times(atime, mtime) -> bool

Sets the last access time and last modified time of the BytesIO.

Returns bool

BytesIO.seek()

io.BytesIO.seek(position, seek_type) -> bool

Sets the position of a BytesIO reader or writer in a BytesIO.

The position must be within the range of the BytesIO size. The seek_type argument must be on of [[io.SEEK_SET]], [[io.SEEK_CUR]] or [[io.SEEK_END]].

Returns bool

BytesIO.tell()

io.BytesIO.tell() -> number

Returns the current position of the reader/writer in the BytesIO.

Returns number

BytesIO.mode()

io.BytesIO.mode() -> string

Returns the mode in which the current BytesIO was opened.

Returns string

BytesIO.name()

io.BytesIO.name() -> string

Returns an empty string since BytesIO do not have a name.

Returns string


2026, Richard Ore and Zuri contributors