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.
- Why Read a File Line-by-Line in Python?
- How to Read a File Line-by-Line in Python
- Conclusion
Table of Contents
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:
- Reduced memory usage: When reading a file line-by-line, only one line of the file is loaded into memory at a time. This can be especially beneficial when working with large files that cannot be loaded into memory all at once.
- Increased speed: Reading a file line-by-line can be faster than reading the entire file at once, especially if the file is very large.
- Flexibility: Reading a file line-by-line allows you to perform operations on each line of the file separately, which can be useful in a variety of scenarios.
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!😇