In this Article we discussed the usage of the readline() method in python and how to run it on a Linux system.
Requirement
Any version of Python should be installed on your system (python3 is preinstalled on the latest Ubuntu system).
Follow the procedure mentioned below to get familiar with readline() method:
Create Python File
To run python programs on Ubuntu, firstly generate a file with “.py” extension (Python file).
Run the below mentioned command to create “python_file.py” to write python programs in it:
The readline() method in Python on Ubuntu
Below mentioned is the syntax of readline() method:
Syntax:
The “file” will be the variable of file, which will be opened to read data from it. The “size” will be used by the readline() method as an argument to read the file till specified size. It is not a mandatory argument. By default it is “-1”.
To use readline() method first we need to open file by using below mentioned syntax:
filename: name of file you want to read
r: it is used to open file in read mode
If you open a file in read mode readline() will return String.
rb(binary mode): you can use binary mode to get binary objects.
After performing the task you need to close the file by below mentioned syntax:
If you are getting a file in a variable then close the file using that variable else you can also close the file by directly using filename.
Read file using readline() without passing size argument in Python
We can read a complete line from a file using the readline() method without specifying size. If we don’t specify size it takes size as -1 by default and returns one complete line. To read one complete line from beginning of file “linuxhint” using python, write the below mentioned code in the “python_file.py”:
print(file.readline())
file.close()
Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file.
Now execute the file created above to get the first complete line of mentioned file by below mentioned command:
Use Readline() Method by Passing Size as Parameter
We can also pass an argument named size (number or integer) to readline() method to read the file till specified size.
To read first 8 characters from file “linuxhint.txt”, write the below mentioned code in the file “python_file.py”:
print(file.readline(8))
file.close()
Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file.
To execute the code written in “python_file.py” to print first 8 characters on terminal, run the below mentioned command:
Read Complete File line by Line Using readline() Method
While loop:
We can get the complete file line by line using readline() method by while loop, run the below mentioned code in “python_file.py” file to get data of “linuxhint.txt” file line by line using while loop:
get_line=file.readline()
while get_line:
print(get_line)
get_line=File.readline()
file.close()
Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file.
To execute the code written in “python_file.py” to print all lines of file “linuxhint.txt” on terminal, run the below mentioned command:
Conclusion:
Python readline() method reads a complete single line from file at a time by default. It has a size argument to specify the number of characters/bytes to read using the readline() method from file. In this article we discuss the use of readline() method in different scenarios like usage of readline() with and without size argument or to read all lines of file. After reading this article you will get a better understanding of the readline() method and will be able to use it in python programs efficiently.