Python readline() Method with Examples

By | January 10, 2021
Python readline() Method

The Python readline() is the Python file handling method. As the method name suggests it only read one complete line from the given file. This method returns the string value along with the new line ("\n") is at the tail of every string.

By default, the Python file readline() method returns the complete line with each call, but we can also specify the size parameter and get the specified number of characters from the line.

Properties of Python file readline() method

Here are some important properties of the Python read line method

  • The readline() the method only reads one complete line at a time.
  • It adds a newline ("\n") at the end of every line.
  • It returns a string value if the file is opened in normal read “r” mode.
  • It returns the binary object if the file opened in the binary mode “b”.
  • It accepts the size parameter that represents the number of characters that should be read from the line.

Python readline() syntax

file.readline(size)

Parameter

size (optional): The size parameter is an optional parameter and by default its value is -1 which means it read and returns the complete line. And the size parameter only accepts integer value.

Return Value of Python readline() method

It returns a complete line in the string format. And with each call, it read the new line from the file.

Python readline() method examples

Now let’s see some examples of readline() method, and read text lines from the example.txt file.

Vamware

Example: Python read 2 lines from a text file

#open file with read mode
with open("example.txt", "r") as file:
    print(file.readline()) #read first line
    print(file.readline())  #read second line

Output

line1 Techgeekbuzz.com
line2 Techgeekbuzz.com

In the above example, you can see that there is a newline gap between line1 and line2. This is because the readline() method also returns the new line("\n") which is at the end of every return string.

If you want to remove this extra line you can use the Python string strip() method and remove the trailing newline.

#open file with read mod
with open("example.txt", "r") as file:
    print(file.readline().strip()) #read first line
    print(file.readline().strip())  #read second line

Output

line1 Techgeekbuzz.com
line2 Techgeekbuzz.com

Example 2: Print all the lines from the text file using readline() method

The readline() method can only read one line at a time. If you want to read all the lines from the text file you need a Python loop. You can either use Python for loop or while loop, both will work fine.

How to read a file line by line using for loop?

#open file with read mode
with open("example.txt", "r") as file:
    for line in file:
        print(line.strip())

Output

line1 Techgeekbuzz.com
line2 Techgeekbuzz.com
line3 Techgeekbuzz.com
line4 Techgeekbuzz.com
line5 Techgeekbuzz.com
line6 Techgeekbuzz.com
line7 Techgeekbuzz.com

In the above example, you can see that you do not even need readline() method to read all the lines from the file. The for loop iterate over the file method line by line.

How to read a file line by line using while loop and readline() method?

#open file with read mode
with open("example.txt", "r") as file:

    line = file.readline()

    while line:
        print(line.strip())
        line = file.readline()

Output

line1 Techgeekbuzz.com
line2 Techgeekbuzz.com
line3 Techgeekbuzz.com
line4 Techgeekbuzz.com
line5 Techgeekbuzz.com
line6 Techgeekbuzz.com
line7 Techgeekbuzz.com

Unlike for loop, in while loop you need to use the readline() method to read all the lines present in the text file.

Example 3: Size parameter of Python file readline(size) method

By default, the readline() method read the one complete line at a time and return it with the "\n" at the end. Sometimes a line could be too long to fit in a display so we can also specify the size parameter to the readline() method and read the specified number of characters with each line.

By specifying the size parameter the readline(size) method does not read specified characters from each line, instead, it divides each line according to the specified size.

#open file with read mode
with open("example.txt", "r") as file:
    print(file.readline(10).strip()) # read first 10 characters from line
    print(file.readline(100).strip()) # read next 100 characters from line

Output

line1 Tech
geekbuzz.com

In this example, you can see that the file.readline(10).strip() statement returns the 10 characters from its line and the statement file.readline(100).strip(), return the next remaining 100 characters from the same line. Although there were not 100 characters left in that line alone but it did not read the next line.

You can also print all the lines from the text file and every line would be under the size of specified length.

#open file with read mode
with open("example.txt", "r") as file:
    line = file.readline(15)  #read only 15 chacracters
    while line:
        print(line.strip())
        line = file.readline(15)  #read only 15 characters

Output

line1 Techgeekb
uzz.com
line2 Techgeekb
uzz.com
line3 Techgeekb
uzz.com
line4 Techgeekb
uzz.com
line5 Techgeekb
uzz.com
line6 Techgeekb
uzz.com
line7 Techgeekb
uzz.com

In the above example, you can see that every line is divided into 15 characters.

Python File readlines() method

The Python readlines() method is used when we want to iterate over the complete content of the file line-by-line.

Python file readlines() function is used with the small text files, it read the complete content of the file in one go and return a list of file line.

Python read lines syntax:

file.readlines(hint)

read lines parameter

The readlines(hints) method accepts the optional parameter hint, which is an integer value. Hint parameter represents the number of bytes. If the number of bytes returned by the readlines() method exceeds the hint number, the readlines() will return no more lines. By default, the value of hint is -1 which signifies return all lines.

Return value of readlines method

The readlines() method returns a list of string lines

#open file with read mode
with open("example.txt", "r") as file:
    print(file.readlines())

Output

['line1 Techgeekbuzz.com\n', 'line2 Techgeekbuzz.com\n', 'line3 Techgeekbuzz.com\n', 'line4 Techgeekbuzz.com\n', 'line5 Techgeekbuzz.com\n', 'line6 Techgeekbuzz.com\n', 'line7 Techgeekbuzz.com']

In the above example, you can see that the readlines() method returns a list of lines. And every line has a trailing new line "\n"except the last line line7 Techgeekbuzz.com.

The above same example of readlines can be performed using file.read() and split()methods

#open file with read mode
with open("example.txt", "r") as file:
    print(file.read().split("\n"))

Output

['line1 Techgeekbuzz.com', 'line2 Techgeekbuzz.com', 'line3 Techgeekbuzz.com', 'line4 Techgeekbuzz.com', 'line5 Techgeekbuzz.com', 'line6 Techgeekbuzz.com', 'line7 Techgeekbuzz.com']

But with read() and split() methods you will not have any trailing newline "\n" with every string element. 

Conclusion

The Python file readline() method or function read the file line-by-line and return one line at a time. It accepts an optional parameter size that divides each line’s characters based on the size value. There is another function readlines() that reads all the lines of a file at one go and returns a list of lines.

Both readline() and readlines() method appends a newline ("\n") at the end of every line of the file. To read all the content of the file at once, you can use the read() method. The readlines() and readline() method are generally used to read the content line-by-line.

People are also reading:

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published. Required fields are marked *