Read a File Line by Line in Python

In Python, you can read a file line by line using methods such as readline(), readlines(), or by iterating directly over the file object. This approach is useful for handling large files efficiently without loading the entire content into memory at once.


Examples

1. Using a for Loop to Read a File Line by Line

The simplest and most memory-efficient way to read a file line by line is by using a for loop.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    for line in file:
        print(line.strip())  # Removing trailing newline characters

Explanation:

  • open("sample.txt", "r"): Opens the file in read mode.
  • with statement: Ensures the file is automatically closed after reading.
  • for line in file: Iterates through each line in the file.
  • line.strip(): Removes any trailing newline characters before printing.

Output (assuming sample.txt contains three lines):

2. Using readline() to Read a File Line by Line

The readline() method reads one line at a time, which is useful when processing files interactively.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    line = file.readline()
    while line:
        print('Line: ' + line.strip())
        line = file.readline()

Explanation:

  • file.readline(): Reads one line at a time.
  • while line: Loops until there are no more lines in the file. Reference: While Loop
  • line.strip(): Removes the newline character before printing.

Output:

3. Using readlines() to Read All Lines into a List

The readlines() method reads all lines at once and returns a list where each element is a line.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    lines = file.readlines()

# Iterating over the list of lines
for line in lines:
    print(line.strip())

Explanation:

  • file.readlines(): Reads all lines into a list.
  • for line in lines: Iterates through the list.
  • line.strip(): Removes trailing newline characters before printing.

Output:

4. Using enumerate() to Read a File with Line Numbers

You can use enumerate() to track line numbers while reading a file.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    for index, line in enumerate(file, start=1):
        print(f"Line {index}: {line.strip()}")

Explanation:

  • enumerate(file, start=1): Tracks line numbers starting from 1.
  • index: Holds the current line number.
  • line.strip(): Removes newline characters before printing.

Output:

Conclusion

Python provides multiple ways to read a file line by line efficiently:

  1. Using a for loop: The most efficient way to read files line by line.
  2. Using readline(): Reads one line at a time inside a loop.
  3. Using readlines(): Reads all lines into a list.
  4. Using enumerate(): Reads lines while tracking line numbers.

For large files, using a for loop is recommended as it processes lines efficiently without loading the entire file into memory.