iobase Python
All I/O flow classes derive from this conceptual base class.
Derived classes will have to execute several of the class's abstract data types.
The loop method is supported by all members of the IOBase class and its derivatives.
As a result, an entity of the IObase class (or one of its subclasses) is able to loop over the incoming data from the underlying flow.
IOBase may be utilized with, and with-as clauses since it also supports the Context Manager Protocol. A fundamental group of operations and characteristics are defined by the IOBase class, which include:
- Close () closes the stream and flushes it.
- If the stream is closed is indicated by the property closed.
- Flush () clears the stream's writing cache if necessary.
- Readable () yields True if data can be interpreted from the stream.
- Readline (size=-1) sends a line back into the stream. If length is given, a maximum of that many bytes will be read.
- Readline (hint=-1) reads the lines in a sequence. The number of lines interpreted is controlled by the hint parameter if it is given.
seek(offset[, whence]) (offset[, whence])
With this technique, the stream location or pointer is moved to the specified distance. The whence variable determines what the offset means.
Whence's possible value is SEEK SET.
- SEEK SET or 0: seek from the beginning of the line (the usual); offset should be a value produced by TextIOBase.tell(), or it needs to be 0. Almost every offset value results in unpredictable behaviour.
- SEEK CUR or 1: "seek" to the present location; the offset should be 0, which constitutes a no-operation.
The primary tools available in Python for handling different I/O types are found in the io package. Text I/O, binary I/O, and raw I/O are the three basic categories of I/O. All of these may be utilized with different supporting stores because they are general categories. A file object is a physical item that fits within one of these classifications. The phrases stream and folder entity are also often used.
Every practical stream object, regardless of classification, will have a variety of features. It may be interpreted, write-only, or read-write. Additionally, it can support just sequential access or unrestricted random access (searching ahead or rearward to any position) (for instance, in the case of a socket or pipe).
All sources are selective regarding the type of information you provide them with. A TypeError will be raised, for instance, if an str object is sent to a binary stream's write() function. Providing a bytes objects to a textual stream's write() function will also work.
Updated in edition 3.3: Because IOError has become a synonym of OSError, procedures that previously raised IOError currently raise OSError.
Text I/O
Text I/O anticipates and generates str entities. This implies that information encoded and decoded, as well as the possible conversion of system-specific newline characters, are handled transparently if the backup repository is made up entirely of bytes (which is the scenario when the supporting store is a file).
Open(), with the choice of an encoding specification, is the simplest method for producing a textual stream:
f = open(''myfile.txt'', ''r'', encoding=''utf-8'')
There are additional in-memory textual streams accessible as StringIO objects:
f = io.StringIO("some initial text data")
The TextIOBase documentary evidence includes a detailed description of the textual stream API.
Binary I/O
Buffered I/O, commonly known as binary I/O, generates bytes instead of the expected bytes-like objects. No newline conversion, encryption, or decryption is carried out. When human control over the treatment of textual data is necessary, this type of stream could be utilized for all types of non-textual data as well.
Using open() mostly with the mode string value of "b" to produce a byte stream is the simplest method:
f = open("myfile.jpg", "rb")
As BytesIO entities, in-memory byte channels are likewise accessible.
f = io.BytesIO(b"some initial binary data: \x00\x01")
The Buffered IOBase documentation goes into great depth on the binary stream API.
Additional techniques to produce text or byte streams could be offered through other collection modules. Take, for instance, socket.socket.makefile().
Raw I/O
Unbuffered I/O, commonly known as raw I/O, is frequently utilized as a low-level structural element for binary and textual streams; client code should seldom directly modify a raw stream. However, opening a document in byte mode with buffering off will allow you to produce a raw stream:
Opening a file in byte mode with buffering off will allow you to produce a raw stream:
f = open("myfile.jpg", "rb", buffering=0)