io
– input/output streams
This module contains additional types of stream (file-like) objects and helper functions.
Conceptual hierarchy
Difference to CPython
The hierarchy of stream base classes is simplified compared to CPython.
Functions
Classes
- class io.StringIO([string])
- class io.BytesIO([string])
In-memory file-like objects for input/output.
StringIO
is used for text-mode I/O (similar to a normal file opened with “t” modifier).BytesIO
is used for binary-mode I/O (similar to a normal file opened with “b” modifier). Initial contents of file-like objects can be specified with the string parameter (should bestr
forStringIO
orbytes
forBytesIO
). All the usual file methods from CPython likeread()
,write()
,seek()
,flush()
andclose()
are available on these objects, and additionally, a following method:- getvalue()
Get the current contents of the underlying buffer which holds data.
- class io.StringIO(alloc_size)
- class io.BytesIO(alloc_size)
Create an empty
StringIO
/BytesIO
object, preallocated to hold up to alloc_size number of bytes. That means that writing that amount of bytes won’t lead to reallocation of the buffer, and thus won’t hit out-of-memory situation or lead to memory fragmentation. These constructors are recommended for usage only in special cases.