Introduction to Python BufferedReader
The following article provides an outline for Python BufferedReader. The class used to provide input buffering which means to fetch the data and store it in the queue of the memory so that when the read() operation is called, it can read the data that is maintained in the queue, to the reader stream is called the Buffered Reader class. And the base class of Buffered Reader class is BufferedIOBase and buffering is necessary for both input streams and output streams in order to achieve efficiency and speed. The buffered reader class consists of three methods namely _init_(Reader_in) method, _init_(Reader_in, iBufSize) method and newline() method.
Syntax:
BufferedReader(inputstream, buffer_size=DEFAULT_BUFFER_SIZE)
- inputstream is the input stream from which the data must be read.
- buffer_size is the size of buffer which is set to default size represented by DEFAULT_BUFFER_SIZE.
Working of BufferedReader in Python
- Whenever there is a need to fetch the data and store it in the queue of the memory so that when the read() operation is called, it can read the data that is maintained in the queue which is called input buffering, we make use of Buffered Reader class in python.
- The Buffered Reader class is used to provide input buffering to the reader stream.
- The Buffered Reader class is derived from its base class BufferedIOBase.
- Buffering is necessary for both input streams and output streams in order to achieve efficiency and speed.
- The buffered reader class consists of three methods namely _init_(Reader_in) method, _init_(Reader_in, iBufSize) method and newline() method.
- Using init_(Reader_in) method, an instance of the Buffered Reader class is created for the given input stream.
- Using _init_(Reader_in, iBufSize) method, an instance of the Buffered Reader class is created for the given input stream along with size of the buffer specified.
- Using readline() method, the data is read from the input stream.
Examples of Python BufferedReader
Given below are the examples mentioned :
Example #1
Python program to demonstrate buffered reader to read the contents of the file.
Code:
#a module called sys in python is imported
import sys
#a module called io in python is imported
import io
#a variable sourcefile is used to store the argument that is passed while running the script. Sys.argv is the argument taking the file name while executing the script and 1 indicates the argument number while 0 being the script that we are executing.
sourcefile = sys.argv[1]
#the file is opened in binary mode to read using open function
with open(sourcefile, 'rb') as file:
#file descriptor is obtained by making using of FileIO which is used to identify the file that is opened
fi = io.FileIO(file.fileno())
#Buffered reader is then used to read the contents of the file
fb = io.BufferedReader(fi)
#the contents of the file is printed
print fb.read(20)
Output:
In the above program, a module called sys in python is imported. Then #a module called io in python is imported. Then a variable source file is used to store the argument that is passed while running the script. Sys.argv is the argument taking the file name while executing the script and 1 indicates the argument number while 0 being the script that we are executing. Then the file is opened in binary mode to read using open function. Then file descriptor is obtained by making using of FileIO which is used to identify the file that is opened. Then Buffered reader is then used to read the contents of the file. Then the contents of the file is printed.
Example #2
Python program to demonstrate buffered reader to read the contents of the file.
Code:
#a module called sys in python is imported
import sys
#a module called io in python is imported
import io
#a variable filename is used to store the argument that is passed while running the script. Sys.argv is the argument taking the file name while executing the script script and 1 indicates the argument number while 0 being the script that we are executing.
filename = sys.argv[1]
#the file is opened in binary mode to read using open function
with open(filename, 'rb') as file:
#file descriptor is obtained by making using of FileIO which is used to identify the file that is opened
fi = io.FileIO(file.fileno())
#Buffered reader is then used to read the contents of the file
fb = io.BufferedReader(fi)
#the contents of the file is printed
print fb.read(20)
Output:
In the above program, a module called sys in python is imported. Then #a module called io in python is imported. Then a variable filename is used to store the argument that is passed while running the script. Sys.argv is the argument taking the file name while executing the script script and 1 indicates the argument number while 0 being the script that we are executing. Then the file is opened in binary mode to read using open function. Then file descriptor is obtained by making using of FileIO which is used to identify the file that is opened. Then Buffered reader is then used to read the contents of the file. Then the contents of the file is printed.
Recommended Articles
This is a guide to Python BufferedReader. Here we discuss the introduction to Python BufferedReader, along with working and programming examples. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses