×

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

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Copying a file from one folder to Another with Python

In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer....

3 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Gethostbyname() function in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Best app for python

What is python? Python is a prevalent, general-purpose, and high-level programming language. Python Programming Language is appropriate for experienced programmers and also for beginners. Python programming language is being utilized by...

19 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.