uio – Input/Output stream functions
This module contains functions and types to work with file objects and other objects similar to files.
Differences to CPython
This module implements a simplified conceptual hierarchy of stream base classes when compared to the CPython implementation.
CPython defines a number of base stream classes which serve as the foundation for the behaviour of the main classes. The SR OS implementation does not define these abstract base classes.
Returned object types
The open() function can be used to open a file. Based on the arguments used
when this function is called, either a TextIO or a FileIO object
is returned.
When a file is opened in text mode, a TextIO object is returned.
This object operates on str data. When a file is opened in binary
mode, a FileIO object is returned. This object operates on
bytes data. The methods of each class are documented independently.
Functions
- uio.open(name, mode='r', buffering=-1, newline=None)
Open a file. The builtin
open()function is aliased to this function.Open a file and return a corresponding file object.
- Parameters:
name (bytes or str) – Absolute name and path to the file that is opened.
mode (str, optional) – Mode in which the file is opened. Supported file modes are shown in the Supported file modes section. Default
rwhich is a synonym ofrt(open for reading text).buffering (int, optional) – Set the buffering policy.
0disables buffering, in both text mode and binary mode. An integer >1indicates the size, in bytes, of a fixed-size chunk buffer. Whenbufferingis1, or when nobufferingargument is given, the file is buffered in chunks of a default size.newline (None or str, optional) – Control the behavior of universal newlines mode. Only applies to
TextIOmode. Accepted options:None, empty string,\n,\r, and\r\n. See the Newline behavior section for more information.
- Returns:
File object. The type of file object returned by the
open()function depends on the mode. Whenopen()is used to open a file in a text mode (w,r,wt,rt, etc.), it returns an instance ofuio.TextIO. When used to open a file in a binary mode an instance ofuio.FileIOis returned, regardless of buffering mode.- Return type:
- Raises:
OSError – File cannot be opened.
Supported file modes
The supported file modes for SR OS are:
Character
Meaning
rOpen for reading (Default)
wOpen for writing, truncating the file first
aOpen for writing, appending to the end of file if it exists
bBinary mode
tText mode (Default)
+Open for updating (reading and writing)
Note
The default mode is r which is a synonym of rt (open for reading text).
Other common values are w for writing (truncating the file if it
already exists) and a for appending.
Note
Modes w+ and w+b open and truncate the file. Modes r+
and r+b open the file with no truncation.
Python distinguishes between binary and text I/O.
Files opened in binary mode (including b in the mode argument of the
open() function) return data as bytes objects without any decoding.
Files opened in text mode (which is the default, or when t is explicitly included in
the mode argument of the open() function) are returned as str, the
bytes having been first decoded as UTF-8.
Newline behavior
The behavior of newlines in the open() function depends on the input provided to the
newline argument.
When reading input from the stream:
If the
newlineargument is set toNone, universal newlines mode is enabled. Lines in the input can end in\n,\ror\r\n. These are translated into\nbefore being returned to the caller.If the
newlineargument is set to an empty string, universal newlines mode is enabled, however, line endings are returned to the caller without translation.If the
newlineargument has any other legal values, input lines are only terminated by the given string and the line ending is returned to the caller without translation.
When writing output to the stream:
If the
newlineargument isNone, any\ncharacters written are translated to the system default line separator,os.linesep.If the
newlineargument is an empty string or\n, no translation takes place.If the
newlineargument is any other legal value, any\ncharacters written are translated to the given string.
Classes
Note
Where a method definition has a / as the last argument, the method takes only
positional arguments. See PEP 570 for more details.
- class uio.TextIO
Text mode object obtained by using the
open()function, for exampleopen(name, "rt").Warning
This class cannot be instantiated directly. Use the
open()function.- read(size=-1, /)
Read at most
sizecharacters from stream. Ifsizeis a negative number or omitted, read until the end of the file (EOF).- Parameters:
size (int, optional) – Maximum number of characters to read from the stream. Default
-1.- Returns:
Characters read from the stream.
- Return type:
str
- Raises:
UnsupportedOperation – File is not readable.
- readinto(b, size=None, /)
Read bytes into a pre-allocated, writable, bytes-like object
b.- Parameters:
b (bytes) – Pre-allocated, writable bytes-like object.
size (int, optional) – The maximum size of bytes to read from the stream. Default
None.
- Returns:
Number of bytes read.
- Return type:
int
- write(text, /)
Write the provided text to the stream.
- Parameters:
text (str) – String to be written to the file.
- Returns:
Number of characters written.
- Return type:
int
- Raises:
UnsupportedOperation – File is not writable.
- seek(offset, whence=0, /)
Change the stream position to the given byte
offsetrelative to the position indicated bywhence.- Parameters:
offset (int) – Byte offset from
whence.whence (int, optional) –
Starting position. Accepted values:
0– Seek from the start of the stream.offsetmust either be a number returned bytell(), or0. Any other offset value produces undefined behaviour.1– Seek to the current position.offsetmust be0, which is a no-operation. All other values ofoffsetare unsupported.2– Seek to the end of the stream.offsetmust be0. All other values ofoffsetare unsupported.
Default
0.
- Returns:
New absolute position.
- Return type:
int
- Raises:
OSError – File does not support random access.
UnsupportedOperation – Nonzero relative seek is unsupported.
- tell()
Obtain the current stream position.
- Returns:
Current stream position.
- Return type:
int
- Raises:
OSError – File does not support random access.
- flush()
Flush the write buffers, if applicable.
- close()
Flush and close the IO object.
This method has no effect if the file is already closed.
- writelines(lines, /)
Write a list of lines to stream.
Line separators are not added. It is normal for each of the lines provided to have a line separator at the end.
- Parameters:
lines (list) – List of lines to be written.
- readline(size=-1, /)
Read until a newline is found or until the end of the file is reached. Return a string containing a single line.
- Parameters:
size (int, optional) – Maximum number of characters to be read.
- Returns:
A single string containing the required line. If the stream is already at the end of the file (EOF), an empty string is returned.
- Return type:
str
- readlines()
Return a list of lines from the stream.
- Returns:
List of lines
- Return type:
list
Differences to CPython
CPython accepts a
hintargument to limit the number of lines read. The SR OS implementation takes no arguments and always reads until the end of the file (EOF).
- readable()
Return whether object is opened for reading.
- Returns:
Whether the file is readable.
Trueindicates that the file can be read,Falseindicates that it cannot.- Return type:
bool
- writable()
Return whether object is opened for writing.
- Returns:
Whether the file is writable.
Trueindicates that the file can be written to;Falseotherwise.- Return type:
bool
- seekable()
Return whether the object supports random access.
- Returns:
Whether the file supports random access (is seekable).
Trueif random access is supported;Falseotherwise.- Return type:
bool
- truncate(size=None, /)
Truncate file to size bytes.
Note
The file pointer is left unchanged.
- Parameters:
size (int, optional) – Size to truncate the file to. Defaults to
Nonewhich is interpreted as the current seek position as reported bytell().- Returns:
The new size of the file.
- Raises:
OSError – File does not support random access.
- buffered: bool
- closed: bool
- mode: str
- name: str
- newlines: str or None
- class uio.FileIO
Binary mode object obtained by using the
open()function, for exampleopen(name, "rb").Warning
This class cannot be instantiated directly. Use the
open()function.- read(size=-1, /)
Read at most
sizebytes from stream. Ifsizeis a negative number or omitted, read until the end of the file (EOF).- Parameters:
size (int, optional) – Maximum number of bytes to read from the stream. Default
-1.- Returns:
Bytes read from the stream.
- Return type:
bytes
- Raises:
UnsupportedOperation – File is not readable.
- readinto(b, size=None, /)
Read bytes into a pre-allocated, writable, bytes-like object
b.- Parameters:
b (bytes) – Pre-allocated, writable bytes-like object.
size (int, optional) – The maximum size of bytes to read from the stream. Default
None.
- Returns:
Number of bytes read.
- Return type:
int
- write(b, /)
Write the provided bytes to the stream.
- Parameters:
b (bytes) – Bytes to be written to the file.
- Returns:
Number of bytes written.
- Return type:
int
- Raises:
UnsupportedOperation – File is not writable.
- seek(offset, whence=0, /)
Change the stream position to the given byte
offsetrelative to the position indicated bywhence.- Parameters:
offset (int) – Byte offset from
whence.whence (int, optional) –
Starting position. Accepted values:
0– From the start of the stream.offsetshould be0or a positive integer.1– From the current position.offsetmay be negative, or positive integer.2– Seek to the end of the stream.offsetis usually a negative integer.
Default
0.
- Returns:
New absolute position.
- Return type:
int
- Raises:
OSError – The file does not support random access.
- tell()
Obtain the current stream position.
- Returns:
Current stream position.
- Return type:
int
- Raises:
OSError – File does not support random access.
- flush()
Flush the write buffers, if applicable.
- close()
Flush and close the IO object.
This method has no effect if the file is already closed.
- writelines(lines, /)
Write a list of lines to stream.
Line separators are not added. It is normal for each of the lines provided to have a line separator at the end.
- Parameters:
lines (list) – List of lines to be written.
- readline(size=-1, /)
Read until a newline is found or until the end of the file is reached. Return a bytes object containing a single line.
- Parameters:
size (int, optional) – Maximum number of bytes to be read.
- Returns:
A single bytes object containing the requested line. If the stream is already at the end of the file (EOF), an empty bytes object is returned.
- Return type:
bytes
- readlines()
Return a list of lines from the stream.
- Returns:
List of lines
- Return type:
list
Differences to CPython
CPython accepts a
hintargument to limit the number of lines read. The SR OS implementation takes no arguments and always reads until the end of the file (EOF).
- readable()
Return whether object is opened for reading.
- Returns:
Whether the file is readable.
Trueindicates that the file can be read;Falseotherwise.- Return type:
bool
- writable()
Return whether object is opened for writing.
- Returns:
Whether the file is writable.
Trueindicates that the file can be written to;Falseotherwise.- Return type:
bool
- seekable()
Return whether the object supports random access.
- Returns:
Whether the file supports random access (is seekable).
Trueif random access is supported.Falseotherwise.- Return type:
bool
- truncate(size=None, /)
Truncate file to size bytes.
Note
The file pointer is left unchanged.
- Parameters:
size (int, optional) – Size to truncate the file to. Defaults to
Nonewhich is interpreted as the current seek position as reported bytell().- Returns:
The new size of the file.
- Raises:
OSError – File does not support random access.
- buffered: bool
- closed: bool
- mode: str
- name: str
- newlines: str or None
- class uio.StringIO([string])
- class uio.StringIO(alloc_size)
In-memory file-like object for input/output use for text-mode I/O, similar to a normal file opened with
tfile mode (See Supported file modes for more information).- Parameters:
string (str) – Initial contents of the file-like object.
alloc_size (int) – Amount of memory to pre-allocate.
- Raises:
OSError – File-like object cannot be created.
The
alloc_sizeconstructor creates an emptyStringIOobject and pre-allocatesalloc_sizebytes. With this constructor, writing up to that many bytes does not reallocate the buffer, so it avoids out-of-memory or memory-fragmentation issues.Warning
The
alloc_sizeconstructor is a specific MicroPython extension and is recommended for use only in special cases and in system-level libraries. It is not recommended for use in end-user applications.- read(size=-1, /)
Read at most
sizecharacters from stream. Ifsizeis a negative number or omitted, read until the end of the stream.- Parameters:
size (int, optional) – Maximum number of characters to read from the stream. Default
-1.- Returns:
Characters read from the stream.
- Return type:
str
- readinto(b, size=None, /)
Read bytes into a pre-allocated, writable, bytes-like object
b.- Parameters:
b (bytes) – Pre-allocated, writable bytes-like object.
size (int, optional) – The maximum size of bytes to read from the stream. Default
None.
- Returns:
Number of bytes read.
- Return type:
int
- write(text, /)
Write the provided text to the stream.
- Parameters:
text (str) – String to be written to the stream.
- Returns:
Number of characters written.
- Return type:
int
- seek(offset, whence=0, /)
Change the stream position to
offsetbytes relative towhence.- Parameters:
offset (int) – Byte offset from
whence.whence (int, optional) –
Starting position. Accepted values:
0– From the start of the stream.offsetshould be0or a positive integer.1– From the current position.offsetmay be negative, or positive integer.2– Seek to the end of the stream.offsetis usually a negative integer.
Default
0.
- Returns:
New absolute position.
- Return type:
int
- tell()
Obtain the current stream position.
- Returns:
Current stream position.
- Return type:
int
- flush()
Flush the write buffers, if applicable.
- close()
Flush and close the IO object.
This method has no effect if the file is already closed.
- readline(size=-1, /)
Read until a newline is found or until the end of the stream is reached. Return a string containing a single line.
- Parameters:
size (int, optional) – Maximum number of characters to be read.
- Returns:
A single string containing the requested line. If the stream is already at the end of the stream, an empty string is returned.
- Return type:
str
- getvalue()
Obtain the current contents of the underlying buffer that holds data.
- Returns:
Contents of the underlying buffer.
- Return type:
str
- class uio.BytesIO([string])
- class uio.BytesIO(alloc_size)
In-memory file-like object for input/output use for binary-mode I/O, similar to a normal file opened with the
bfile mode (See Supported file modes for more information).- Parameters:
string (bytes) – Initial contents of the file-like object.
alloc_size (int) – Amount of memory to pre-allocate.
- Raises:
OSError – File-like object cannot be created.
The
alloc_sizeconstructor creates an emptyBytesIOobject and pre-allocatesalloc_sizebytes. With this constructor, writing up to that many bytes does not reallocate the buffer, so it avoids out-of-memory or memory-fragmentation issues.Warning
The
alloc_sizeconstructor is a specific MicroPython extension and is recommended for use only in special cases and in system-level libraries. It is not recommended for use in end-user applications.- read(size=-1, /)
Read at most
sizebytes from stream. Ifsizeis a negative number or omitted, read until the end of the stream.- Parameters:
size (int, optional) – Maximum number of bytes to read from the stream. Default
-1.- Returns:
Bytes read from the stream.
- Return type:
bytes
- read1(size=-1, /)
Read at most
sizebytes from stream using at most one underlying read operation. Ifsizeis a negative number or omitted, read until the end of the stream.- Parameters:
size (int, optional) – Maximum number of bytes to read from the stream. Default
-1.- Returns:
Bytes read from the stream.
- Return type:
bytes
- readinto(b, size=None, /)
Read bytes into a pre-allocated, writable, bytes-like object
b.- Parameters:
b (bytes) – Pre-allocated, writable bytes-like object.
size (int, optional) – The maximum size of bytes to read from the stream. Default
None.
- Returns:
Number of bytes read.
- Return type:
int
- readinto1(b, size=None, /)
Read bytes into a pre-allocated, writable, bytes-like object
b, using at most one underlying read operation.- Parameters:
b (bytes) – Pre-allocated, writable bytes-like object.
size (int, optional) – The maximum size of bytes to read from the stream. Default
None.
- Returns:
Number of bytes read.
- Return type:
int
- write(b, /)
Write the provided bytes to the stream.
- Parameters:
b (bytes) – Bytes to be written to the stream.
- Returns:
Number of bytes written.
- Return type:
int
- seek(offset, whence=0, /)
Change the stream position to the given byte
offsetrelative to the position indicated bywhence.- Parameters:
offset (int) – Byte offset from
whence.whence (int, optional) –
Starting position. Accepted values:
0– From the start of the stream.offsetshould be0or a positive integer.1– From the current position.offsetmay be negative, or positive integer.2– Seek to the end of the stream.offsetis usually a negative integer.
Default
0.
- Returns:
New absolute position.
- Return type:
int
- tell()
Obtain the current stream position.
- Returns:
Current stream position.
- Return type:
int
- flush()
Flush the write buffers, if applicable.
- close()
Flush and close the IO object.
This method has no effect if the file is already closed.
- readline(size=-1, /)
Read until a newline is found or until the end of the stream is reached. Return a bytes object containing a single line.
- Parameters:
size (int, optional) – Maximum number of bytes to be read.
- Returns:
A single bytes object containing the requested line. If the stream is already at the end of the stream, an empty bytes object is returned.
- Return type:
bytes
- getvalue()
Obtain the current contents of the underlying buffer that holds data.
- Returns:
Contents of the underlying buffer.
- Return type:
bytes
Exceptions
- exception uio.UnsupportedOperation
Exception raised when an unsupported operation is attempted on a stream.
Examples include reading from a file that is not readable, writing to a file that is not writable, or performing a nonzero relative seek on a
TextIOobject.Differences to CPython
In CPython, this exception inherits from both
OSErrorandValueError. The SR OS implementation inherits fromException.