Read Text File Line by Line in Python


Reading a file line-by-line is a common task in Python, especially when working with large files that cannot be loaded into memory all at once.

Python provides several ways to read files, but in this guide, we will focus on the most efficient and widely used method of reading files line-by-line.

    Table of Contents

  1. Why Read a File Line-by-Line in Python?
  2. How to Read a File Line-by-Line in Python
    1. using readline() method
    2. using readlines() method
    3. using for loop
    4. using with statement
  3. Conclusion

Why Read a File Line-by-Line in Python?

Reading a file line-by-line has several advantages over reading the entire file at once. Some of these advantages include:


How to Read a File Line-by-Line in Python

There are several ways to read a file line-by-line in Python. In this guide, we will focus on the most efficient and widely used method of reading files line-by-line.

Let's start by creating a text file named file.txt with the following contents:

Line 1
Line 2
Line 3
Line 4
Line 5

Now, let's see how to read this file line-by-line in Python.

1. Using the readline() Method

The readline() method reads a single line from a file. It returns an empty string when the end of the file is reached.

Let's see how to use the readline() method to read the file line-by-line:

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

# Read the first line
line = file.readline()

# If the file is not empty keep reading line one at a time
# till the file is empty
while line:
    print(line, end='')
    # read next line
    line = file.readline()

file.close()

Output:

Line 1
Line 2
Line 3
Line 4
Line 5

2. Using the readlines() Method

The readlines() method reads the entire file into memory and returns a list of lines.

To read a file line-by-line using the readlines() method, we can use a for loop to iterate over the list of lines:

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

# Read all lines at once
lines = file.readlines()

# Iterate over the lines
for line in lines:
    print(line, end='')

file.close()

Output:

Line 1
Line 2
Line 3
Line 4
Line 5

Note: The readlines() method is not recommended for large files as it loads the entire file into memory.


3. Using the for loop Method

Another way to read a file line-by-line in Python is to use a for loop to iterate over the file object.

When you iterate over a file object, the for loop automatically reads one line at a time from the file.

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

# Iterate over the file object using a for loop
for line in file:
    print(line, end='')

file.close()

Output:

Line 1
Line 2
Line 3
Line 4
Line 5

4. Using the with Statement

The with statement is a safe way to handle a file. It ensures that the file is closed when the block inside the with statement is exited.

Let's see how to use the with statement to read a file line-by-line in Python:

with open("file.txt", "r") as file:
    # Iterate over the file object using a for loop
    for line in file:
        print(line, end='')

Output:

Line 1
Line 2
Line 3
Line 4
Line 5

Conclusion

We learned how to read a file line-by-line in Python using multiple methods.

Now that you know how to read a file line-by-line in Python, you can use this knowledge to build more complex programs that can read and process large files.

Happy learning!😇