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.

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-1.png" data-lazy- height="224" src="data:image/svg xml,” width=”930″>

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:

file_variable/filename.close()

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

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

print(file.readline())

file.close()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-2.png" data-lazy- height="834" src="data:image/svg xml,” width=”931″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-3.png" data-lazy- height="253" src="data:image/svg xml,” width=”925″>

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

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

print(file.readline(8))

file.close()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-4.png" data-lazy- height="831" src="data:image/svg xml,” width=”933″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-5.png" data-lazy- height="240" src="data:image/svg xml,” width=”932″>

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:

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


get_line=file.readline()

while get_line:


    print(get_line)


    get_line=File.readline()

file.close()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-6.png" data-lazy- height="834" src="data:image/svg xml,” width=”930″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/How-to-use-Python-readline-7.png" data-lazy- height="675" src="data:image/svg xml,” width=”929″>

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.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/7c99efcc024949e17b176845c757687e?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

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.