How to use Python readline() function

There are many useful methods of Python which we can use in our programs. One of the handy methods is python readline() method, it reads one complete line from a specified file at a time by default. You can also read more than one line or complete file using readline() method using different conditions like using size argument. The readline() method inserts a new line (\n) at the end of returned String.

In this Article we discussed the usage of the readline() method in python and how to run it on a Linux system.


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 “” to write python programs in it:

$ nano

The readline() method in Python on Ubuntu

Below mentioned is the syntax of readline() method:



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 “”:

file=open("linuxhint.txt ","r ")

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:

$ python3

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 “”:

file=open("linuxhint.txt ", "r ") >>use small f change screenshot<<

Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file.

To execute the code written in “” to print first 8 characters on terminal, run the below mentioned command:

$ python3

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 “” file to get data of “linuxhint.txt” file line by line using while loop:

file=open("linuxhint.txt ", "r ")  
while get_line:

Press “Ctrl+s” to save the file and “Ctrl+x” to exit the file.

To execute the code written in “” to print all lines of file “linuxhint.txt” on terminal, run the below mentioned command:

$ python3


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.

About the author

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.