×

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)


Related Topics

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Colors in Python

Adding colour to your visualisations will help them come to life. Even if you know the colours you want to use, picking good ones and putting them into practise might...

4 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python sum() function

Python sum() function The sum() function in python sums start and the items of an iterable from left to right and returns the total.  Syntax sum(iterable[, start]) Parameter iterable: This parameter represents the sequence to sum. start: It is optional...

1 minute read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Salary of Python Developers in India

In this tutorial, we will understand who python developers are and what is their salary when they work in India. Python Developers Python Developers are the people who are involved in designing...

4 minutes read.

Python Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.