×

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 Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Python Num2words

Python: 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 is said...

3 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Create First GUI Application using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Captcha Code in Python with Example

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.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Get index of element in array in python

Index : Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also...

4 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.