Python provides several modules and functions for handling input and output operations related to files, such as io, shutil, and many more. In Python, the βioβ module supports several modules and functions for dealing with binary data, Unicode data, and string data.
The outcomes of this tutorial are:
- What is the Python IO Module?
- BytesIO Class of IO Module in Python
- StringIO Class of IO Module in Python
What is the Python IO Module?
In Python, the βioβ module provides various sets of classes and functions for managing files related to input and output operations. One of the advantages of utilizing the βioβ module is that it allows us to extend its functionality to enable writing to Unicode data. We can also use various classes and functions of the βioβ module for Unicode encoding and decoding.
Python offers the IO module for handling stream and buffer operations in various ways. There are two useful classes within the IO module, such as βBytesIOβ and βStringIOβ, which can be accessed using the βio.BytesIOβ and βio.StringIOβ.
Letβs get started with the BytesIO class!
BytesIO Class of IO Module in Python
The βBytesIOβ class in Python provides a stream-like interface for handling binary data in memory. This class is used to treat bytes data as a file-like object that enables read and write operations on the bytes without using the physical file. To get more understanding, have a look at the following code:
bs = io.BytesIO(b'Linuxhint\x0AGuide')
print(bs, '\n')
print(bs.getvalue())
bs.close()
In the above example code:
- First, import the βioβ module.
- Then, use the βio.BytesIO()β constructor that takes the binary data as an argument and creates the BytesIO object.
- Next, print the BytesIO object on the console utilizing the βprint()β function.
- To retrieve and print the content of the BytesIO object, use the βgetvalue()β method.
- Finally, utilize the βclose()β method to close the BytesIO object.
As you can see, the BytesIO object, along with its value, has been created and printed on the console:
StringIO Class of IO Module in Python
In Python, the βStringIOβ class is similar to the βBytesIOβ class, and it is used to treat strings as file-like objects rather than bytes. This class delivers methods for reading from and writing to an in-memory buffer. For further understanding, letβs have a look at the below examples:
Example 1: Creating and Reading Data from File Object
Check out the below-provided code to create a file object and read data from it:
str1 = 'Linuxhint Guide'
file1 = io.StringIO(str1)
file2 = file1.read()
print(file2)
Here:
- Initially, imported the βioβ module and then used the βio.StringIO()β constructor to create the file-like string object.
- Next, invoked the βfile.read()β method to read the newly created data and display it to the console using the βprint()β function.
Output
The data has been created and displayed successfully on the console.
Example 2: Writing Data to File Object
Letβs take an example for writing a string to the newly created file object:
str1 = 'Linuxhint Guide'
file1 = io.StringIO(str1)
file2 = file1.read()
file1.write(' Welcome to Python Guide')
file1.seek(0)
file3 = file1.read()
print(file3)
According to the above-provided code:
- First of all, added the βioβ module.
- Then, use the βio.StringIO()β function to create an in-memory buffer object (file object) by taking the string as an argument.
- Next, apply the βread()β method to read the contents of the object named βfile1β.
- Afterward, utilized the βwrite()β method to write or append the string to the existing content in the in-memory buffer or file object.
- To shift the file handler/pointer to the start of the file, invoke the βseek()β method.
- Finally, use the βread()β method to read all the content of the file and display it to the console using the βprint()β function.
It can be seen that the string has been successfully written to the string file-like object:
Example 3: Using βio.open()β Method to Read a File
The βio.open()β method can also be used to read the data from the file that is similar to the file object. Here is the snippet of the file along with its original content:
To read data from the file using the buffered read, the below example is used in Python:
f1 = io.open("example.txt", "rb", buffering = 5)
print(f1.read())
f1.close()
In the above code:
- The βioβ module is imported/added at the start.
- Then, use the βio.open()β method to open the file in a binary module for reading the data from the file.
- Next, pass the βrbβ mode and βbuffering=5β parameters to this particular method. These parameters specify the mode and the buffer size to be used for reading the file. Here based on the buffer size, data will be read in chunks of 5 bytes at a time.
- Lastly, the βread()β method is used to read the entire content of the file, and the βclose()β method is utilized to terminate/close the file.
Output
The data has been read successfully.
Conclusion
In Python, the βIOβ module is used to provide several functions and classes for handling and performing input/output operations on data. There are two main classes of the βInput/Outputβ module, such as BytesIO and StringIO. The βBytesIOβ class is used to handle and manipulate the binary data, and the βStringIOβ class is used to work with string data. In this guide, we have delivered a complete overview of the Python IO module using numerous examples.