PythonPandas.com

Read file line by line in Python



Reading a file line by line in Python is a common task when dealing with text files, logs, or large datasets.
It’s efficient, memory-friendly, and allows you to process each line individually without loading the entire file into memory.

In this article, you’ll learn multiple ways to read files line by line in Python, including examples using for loops, readline(), readlines(), and context managers.

Method 1: Using a for Loop (Best & Pythonic Way)

# Open file and iterate line by line
with open('sample.txt', 'r') as file:
for line in file: print(line.strip())

Output:

 Line 1: Hello World Line 2: Python is awesome Line 3: File handling made easy 

Why use this?
> Most Pythonic and efficient method
> Automatically handles large files (reads line by line)
> Closes the file automatically using with statement

Method 2: Using readline() in a Loop

readline() reads one line at a time. This gives you fine-grained control over how each line is processed.

file = open('sample.txt', 'r')

line = file.readline()
while line:
print(line.strip())
line = file.readline()

file.close()

Output:

 Line 1: Hello World Line 2: Python is awesome Line 3: File handling made easy 

Method 3: Using readlines() (For Smaller Files)

readlines() reads all lines into a list — not memory-efficient for very large files, but simple for small ones.


with open('sample.txt', 'r') as file:
lines = file.readlines()

for line in lines:
print(line.strip())

Output:

 Line 1: Hello World Line 2: Python is awesome Line 3: File handling made easy 

Method 4: Using enumerate() to Track Line Numbers

You can easily print line numbers while reading a file.


with open('sample.txt', 'r') as file:
for line_number, line in enumerate(file, start=1):
print(f"Line {line_number}: {line.strip()}")

Output:

 Line 1: Hello World Line 2: Python is awesome Line 3: File handling made easy 

Method 5: Read File Lines into a List Using List Comprehension

with open('sample.txt', 'r') as file:
lines = [line.strip() for line in file]

print(lines)

Output:

['Line 1: Hello World', 'Line 2: Python is awesome', 'Line 3: File handling made easy']

Method 6: Using fileinput Module (Advanced)

The fileinput module is great when you want to read from multiple files seamlessly.

import fileinput

for line in fileinput.input(files=('sample.txt', 'log.txt')):
print(line.strip())

Output:

 Line 1: Hello World Line 2: Python is awesome Line 3: File handling made easy ... 

Why use this?
> Reads multiple files as if they were a single stream
> Excellent for merging or analyzing log files

Method 7: Handling Large Files Efficiently

For very large files, use generators or buffered reading to reduce memory usage.


def read_large_file(file_path):
with open(file_path, 'r') as f:
for line in f:
yield line.strip()

for line in read_large_file('large_dataset.txt'):
print(line)

Output:

Prints each line without loading the entire file into memory.

Why use this?
> Extremely memory-efficient
> Perfect for reading multi-GB log or dataset files

Read Lines and Skip Empty or Comment Lines


with open('config.txt', 'r') as file:
for line in file: stripped = line.strip() if stripped and not stripped.startswith('#'): print(stripped)

Output:

Prints only non-empty, non-comment lines.

Why use this?
> Useful for reading config or code files
> Skips blank lines and comments automatically.

FAQs — Python: Read a File Line by Line

How to read a file line by line in Python using a for loop?

Use a for loop directly on the file object for efficient line-by-line reading:

with open('data.txt', 'r') as f:
    for line in f:
        print(line.strip())

This reads one line at a time without loading the entire file into memory.

How to read a file line by line in Python using readline()?

Use readline() inside a while loop to manually control reading:

f = open('data.txt', 'r')
line = f.readline()
while line:
    print(line.strip())
    line = f.readline()
f.close()

This approach gives more control but requires manually closing the file.

How to read all lines of a file into a list in Python?

Use readlines() to load all lines at once:

with open('data.txt', 'r') as f:
    lines = f.readlines()
print(lines)

Each line is stored as a string element in a list.

How to read a large file line by line without using too much memory?

Use a generator expression or iterate directly over the file object:

with open('large_file.txt', 'r') as f:
    for line in f:
        process(line)

This method is memory-efficient, even for very large files.

How to read a file line by line and remove newline characters in Python?

Use strip() or rstrip() to clean up trailing newlines:

with open('data.txt', 'r') as f:
    for line in f:
        print(line.strip())

This removes unwanted \n characters at the end of each line.

How to read a file line by line in Python using enumerate() to track line numbers?

Combine enumerate() with file iteration:

with open('data.txt', 'r') as f:
    for i, line in enumerate(f, start=1):
        print(f"Line {i}: {line.strip()}")

This lets you display or process line numbers alongside content.

How to read a file line by line using Pathlib in Python?

Use Path.read_text() and splitlines() from the pathlib module:

from pathlib import Path

lines = Path('data.txt').read_text().splitlines()
for line in lines:
    print(line)

This gives a clean and modern approach using Pathlib.

How to read specific lines from a file in Python?

Read all lines and use indexing or slicing:

with open('data.txt', 'r') as f:
    lines = f.readlines()

print(lines[0])      # First line
print(lines[2:5])    # Lines 3 to 5

How to handle encoding errors while reading files line by line in Python?

Specify the file encoding and error handling behavior:

with open('data.txt', 'r', encoding='utf-8', errors='ignore') as f:
    for line in f:
        print(line)

This prevents UnicodeDecodeError when dealing with non-UTF files.


Related Post