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

</>
Copy
# 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:

  1. open("example.txt", "r"): Opens the file in read mode.
  2. file: The file object representing the opened file.
  3. for line in file: Iterates through each line in the file.
  4. 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

</>
Copy
# 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:

  1. file.readline(): Reads one line at a time from the file.
  2. while line: Continues reading until an empty string ("") is returned, indicating the end of the file.
  3. 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

</>
Copy
# 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:

  1. file.readlines(): Reads all lines at once into a list.
  2. for line in lines: Iterates over the list of lines.
  3. 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

</>
Copy
# 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:

  1. file.read(): Reads the entire file as a string.
  2. .splitlines(): Splits the string into a list of lines without newline characters.
  3. 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:

  1. for line in file: The most efficient way to iterate over a file.
  2. readline(): Reads one line at a time manually.
  3. readlines(): Loads all lines into a list.
  4. 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.