Table of contents
1.
Introduction
2.
What is readlines() function in Python?
3.
Syntax 
4.
Parameters
4.1.
Basic Usage
5.
Examples of readlines() function in Python 
5.1.
Method 1: Read a File Line by Line using readline() :
5.2.
Method 2: Read a File Line by Line using readlines()
5.3.
Method 3: Read a File Line by Line using for loop
5.4.
Method 4: Read a File Line by Line using for loop and list comprehension
6.
readline() vs readlines()
7.
Advantages of Python Readiness
8.
Disadvantages of Python Readiness
9.
Future of readlines()
10.
Frequently Asked Questions
10.1.
What is the readline() method in Python?
10.2.
What does the Readlines ()> method returns?
10.3.
How do you use Readlines function?
10.4.
What is the difference between readline () and Readlines ()?
11.
Conclusion
Last Updated: Aug 13, 2025
Easy

Python Readlines

Author Riya Singh
0 upvote

Introduction

Python File readlines() Method reads until end-of-file (EOF) using readline() and returns a list containing the lines.

python readlines

This article aims to provide a deep dive into the readlines() method, exploring its syntax, parameters, and various use cases. By the end of this read, you will have a comprehensive understanding of how to leverage readlines() for your file processing tasks.

What is readlines() function in Python?

The readlines() function in Python is used with file objects to read all the lines from a file into a list. Each line in the file becomes an element in the list, with the newline character \n at the end of each element. This function is particularly useful for iterating over each line in a file or when you need to process multiple lines together. For example, after opening a file in read mode, calling file.readlines() will return a list where each item is a line from the file. This allows for easy manipulation of file data within Python scripts.

Syntax 

The readlines() method is associated with file objects and is used to read the contents of a file into a list where each line of the file is a separate element in the list.

file_object.readlines(hint)

Also see, Python Operator Precedence

Parameters

This is an optional parameter which specifies the number of bytes to be read from the file. By default, it's set to -1, indicating that the whole file should be read.

Basic Usage

The primary use of readlines() is to read all the lines of a file and return them as a list of strings.

# Opening a file

with open('example.txt', 'r') as file:
    lines = file.readlines()
    print(lines)

 

Handling Large Files:

For large files, the hint parameter can be utilized to control the number of bytes read from the file, helping in memory management.

with open('large_file.txt', 'r') as file:
    partial_lines = file.readlines(100)
    print(partial_lines)

In this example, only the first 100 bytes of the file are read and returned as a list of lines.

In the below article, we are going to study Python readlines() line by line from a file.

Examples of readlines() function in Python 

 

Method 1: Read a File Line by Line using readline() :

# Writing to a sample file 'example_readline.txt'
with open('example_readline.txt', 'w') as file:
   file.write("Line 1: Hello, World!\n")
   file.write("Line 2: This is a test file.\n")
   file.write("Line 3: Each line will be read individually.\n")
   file.write("Line 4: Using readline() method.")
# Reading the file line by line using readline()
lines_read = []
with open('example_readline.txt', 'r') as file:
   while True:
       line = file.readline()
       if not line:
           break
       lines_read.append(line.strip())  # Using strip() to remove newline characters
# Printing each line from the file
for line in lines_read:
   print(line)
You can also try this code with Online Python Compiler
Run Code

Method 2: Read a File Line by Line using readlines()

# Writing to a sample file 'example_readlines.txt'
with open('example_readlines.txt', 'w') as file:
   file.write("Line 1: Hello, again!\n")
   file.write("Line 2: We are using readlines() this time.\n")
   file.write("Line 3: Each line is read into a list.\n")
   file.write("Line 4: Easy and efficient.")
# Reading the file line by line using readlines()
with open('example_readlines.txt', 'r') as file:
   lines = file.readlines()
# Processing each line from the file
for line in lines:
   print(line.strip())  # Using strip() to remove newline characters
You can also try this code with Online Python Compiler
Run Code

Method 3: Read a File Line by Line using for loop

# Writing to a sample file 'example_for_loop.txt'
with open('example_for_loop.txt', 'w') as file:
   file.write("First Line: Using for loop.\n")
   file.write("Second Line: Each line is processed as it's read.\n")
   file.write("Third Line: Efficient for large files.\n")
   file.write("Fourth Line: No need to load everything into memory.")
# Reading the file line by line using a for loop
lines_read_for_loop = []
with open('example_for_loop.txt', 'r') as file:
   for line in file:
       lines_read_for_loop.append(line.strip())  # Using strip() to remove newline characters
# Printing each line from the file
for line in lines_read_for_loop:
   print(line)
You can also try this code with Online Python Compiler
Run Code

Method 4: Read a File Line by Line using for loop and list comprehension

# Writing to a sample file 'example_list_comp.txt'
with open('example_list_comp.txt', 'w') as file:
   file.write("Line 1: List Comprehension method.\n")
   file.write("Line 2: Concise and efficient.\n")
   file.write("Line 3: Suitable for compact scripts.\n")
   file.write("Line 4: Pythonic way to read files.")
# Reading the file line by line using for loop and list comprehension
with open('example_list_comp.txt', 'r') as file:
   lines_read_list_comp = [line.strip() for line in file]  # Using list comprehension to process each line
# Printing each line from the file
for line in lines_read_list_comp:
   print(line)
You can also try this code with Online Python Compiler
Run Code

readline() vs readlines()

Feature/Aspect readline() readlines()
Functionality Reads one line from the file at a time. Reads all lines from the file at once.
Return Type Returns a single line as a string. Returns a list, with each line as an element.
Memory Efficiency It is more efficient, as it reads one line at a time. It is less efficient with large files, as it loads all lines into memory.
Use Case Ideal for large files or when only part of the file is needed. It's preferred when you need to process the whole file and require line-by-line processing that is not excessively large.
Iteration Typically used in a loop to read lines sequentially. Directly provides all lines for immediate iteration or processing.
End of File Handling Returns an empty string when the end of the file is reached. It reads until EOF and then stops.
Typical Usage Used in scenarios where line-by-line processing is required, such as parsing large log files. Used when the file size is manageable, requiring all lines to be needed simultaneously, like reading configuration files.

Advantages of Python Readiness

Simple and Easy to Use: readlines() provides a straightforward way to read all lines of a file into a list, making it easy to work with.

Effective for Small to Medium Files: It's an efficient method when working with relatively small to medium-sized files.

Disadvantages of Python Readiness

Memory Consumption: For large files, readlines() can consume a lot of memory as it loads the entire file into memory.

Future of readlines()

The readlines() method has been a part of Python's file handling toolkit for a long time and continues to serve its purpose effectively. However, with the advent of more memory-efficient methods and libraries, such as pandas for data processing, the use of readlines() might see a decline for large-scale data processing tasks.

Frequently Asked Questions

What is the readline() method in Python?

Python's readline() method is used to read a single line from a file. It reads the line to the newline character and returns it as a string, making it ideal for sequential file reading.

What does the Readlines ()> method returns?

The readlines() method in Python returns a list containing all the lines in a file, with each line as a separate string element, including newline characters at the end of each line.

How do you use Readlines function?

To use the readlines function in Python, open a file in read mode, call readlines() on the file object, and it will return a list of lines from the file, each line as a separate list item.

What is the difference between readline () and Readlines ()?

readline() reads and returns a single line from a file, while readlines() reads all lines in a file and returns them as a list of strings, with each list item representing one line.

Conclusion

The readlines() method is a fundamental part of Python’s file handling suite, providing a simple and effective way to read text files line by line. Understanding its usage, advantages, and limitations is crucial for anyone looking to master file processing in Python. As we step into an era of big data, exploring memory-efficient alternatives alongside readlines() could be a prudent approach for handling large-scale data processing tasks.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass