Iterate Over a File Line by Line Using a Loop in Python
To read a file line by line in Python, you can use a loop with methods like readline()
, readlines()
, or iterate directly over the file object. This is useful when working with large files as it allows processing each line without loading the entire file into memory.
Examples
1. Using a for
Loop to Read a File Line by Line
One of the most efficient ways to read a file line by line is by using a for
loop to iterate directly over the file object.

main.py
# Open the file in read mode
with open("example.txt", "r") as file:
# Iterate over each line
for line in file:
print(line.strip()) # Print the line after stripping extra whitespace
Explanation:
open("example.txt", "r")
: Opens the file in read mode.file
: The file object representing the opened file.for line in file
: Iterates through each line in the file.line.strip()
: Removes leading/trailing whitespace, including newlines.
Output:

2. Using readline()
to Read One Line at a Time
The readline()
method reads one line at a time, making it useful when you need more control over the reading process.

main.py
# Open the file in read mode
with open("example.txt", "r") as file:
line = file.readline() # Read the first line
while line:
print(line.strip()) # Print the line after stripping extra whitespace
line = file.readline() # Read the next line
Explanation:
file.readline()
: Reads one line at a time from the file.while line
: Continues reading until an empty string (""
) is returned, indicating the end of the file.line.strip()
: Removes extra whitespace, including newline characters.
Output:

3. Using readlines()
to Store All Lines in a List
The readlines()
method reads all lines from the file at once and returns them as a list.

main.py
# Open the file in read mode
with open("example.txt", "r") as file:
lines = file.readlines() # Read all lines into a list
for line in lines:
print(line.strip()) # Print each line after stripping whitespace
Explanation:
file.readlines()
: Reads all lines at once into a list.for line in lines
: Iterates over the list of lines.line.strip()
: Removes leading/trailing whitespace.
Output:

4. Using file.read()
with splitlines()
to Read File Line by Line
The splitlines()
method splits the entire file content into a list of lines, similar to readlines()
but without newline characters.

main.py
# Open the file in read mode
with open("example.txt", "r") as file:
lines = file.read().splitlines() # Read entire file and split by lines
for line in lines:
print(line)
Explanation:
file.read()
: Reads the entire file as a string..splitlines()
: Splits the string into a list of lines without newline characters.for line in lines
: Iterates over the list of lines and prints them.
Output:

Conclusion
In this tutorial, we covered multiple ways to read a file line by line:
for line in file
: The most efficient way to iterate over a file.readline()
: Reads one line at a time manually.readlines()
: Loads all lines into a list.splitlines()
: Reads the entire file as a string and splits by lines.
The best method depends on your use case. If handling large files, iterating over the file object directly is recommended to save memory.