"
This article is part of in the series
Published: Friday 1st March 2024

How To Use The Python Readlines() Method: A Beginner's Guide

Python offers several ways to read files. The readlines() function is one of them. It allows Python to accept a text file as input and then store every line of text in the file as an element of a list.

This function reads text files until it reaches the end-of-file (EOF). Then, it returns the list it creates containing all the text lines as list elements. 

In this short guide, we will explore how the readlines() function works. We will explore its syntax, the parameters that work with it, and its varied use cases. 

How to Use the readlines() Function

The readlines() method makes every line in a text file an element in the list. Moreover, it adds a newline character at the end of every element. 

For this reason, developers use the readlines() function when they need to iterate over the lines in a file object. Additionally, the function is helpful when you need to process several lines. 

Let's assume that you need to modify the data of a file with Python. To do this, call the readlines() method on the file and open it in read mode. As you’d expect, readlines() returns a list where the elements are the lines from the file.

To use this function, you must follow this syntax:

fileName.readlines(sizehint)

 

As you can see, the function can accept one optional parameter, "sizehint." This parameter represents the number of bytes you wish to read from the file, rounded to the nearest internal buffer size.

If you don’t use this optional parameter, the function reads the file until it reaches the EOF. Then, it returns an empty string.

Basic Uses of the readlines() Function

Let's see how you can use readlines() to read the lines from an open file and capture the lines in it as a list of strings:

with open('sampleText.txt', 'r') as file:

    stringList = file.readlines()

    print(stringList)

 

You can work with large files using the sizehint parameter. The parameter will help with memory management by controlling the number of bytes Python reads from the text file. 

Let's see this in action:

with open('hugeFile.txt', 'r') as file:

    limitedReading = file.readlines(100)

    print(limitedReading)

 

The program above will only read the first 100 bytes of a file and then return a list of lines. 

Differences Between readline() and readlines()

The key difference between the readline() function and the readlines() function is that readline() only returns a single line from the file.

However, there are more noteworthy differences between them:

readline () readlines ()
Function Reads only one line from a file.  Reads all lines from a file.
Return Type Returns a single line as a
string.
Returns a list with lines from the file as elements.
Efficiency It is highly efficient as it reads one line only. Low memory efficiency with large files.
Use Case Best used when only part of a large file is needed. Ideal for processing entire files line-by-line. Works best when files aren't excessively large.
Iteration  Typically used in a loop to read lines sequentially. Provides access to all lines for immediate iteration.
EOF Handling Returns an empty string at EOF. Reads the file until EOF and then stops.