Introduction to Python BufferedReader
The following article provides an outline for Python BufferedReader. It retrieves data from a source and stores it in an internal memory queue. This allows subsequent read operations to efficiently retrieve data from the buffered queue instead of directly accessing the underlying reader stream. 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 the buffer which is set to the default size represented by DEFAULT_BUFFER_SIZE.
Working of BufferedReader in Python
- The Buffered Reader class provides input buffering to the reader stream.
- The Buffered Reader class derives from its base class, BufferedIOBase, to utilize its functionality and properties.
- The buffered reader class consists of three methods, namely _init_(Reader_in) method, _init_(Reader_in, iBufSize) method, and newline() method.
- The __init__(Reader_in) method is utilized to instantiate an object of the BufferedReader class, which receives the given input stream as a parameter.
- The readline() method retrieves data from the input stream. It reads a line of data from the stream and returns it as a string.
Examples of Python BufferedReader
Given below are the examples mentioned :
Example #1
Python program to demonstrate a buffered reader to read the file’s contents.
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:
This module allows access to system-specific parameters and functions. Sys. argv is the argument taking the file name while executing the script, 1 indicates the argument number, and 0 is the script we execute. The program opens the file in binary mode using the open function to enable reading from it. It obtains a file descriptor by creating a FileIO object, which identifies the opened file. Then, it uses a BufferedReader to read the contents of the file. Finally, it prints the contents of the file.
Example #2
Python program to demonstrate a buffered reader to read the file’s contents.
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:
Sys.argv is the argument taking the file name while executing the script, and 1 indicates the argument number, while 0 is the script we execute. The program opens the file in binary mode using the open function to read it. It obtains a file descriptor by creating a FileIO object, which identifies the open file. Then, it uses a BufferedReader to read the contents of the file. Finally, it prints the contents of the file.
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 –