Last modified: Feb 06, 2026 By Alexander Williams

Python Read File Line by Line Tutorial

Reading files is a core skill in Python programming. You often need to process data from text files. Doing this line by line is efficient. It prevents your program from using too much memory.

This guide will show you the best methods. You will learn to use readline(), for loops, and context managers. We will cover each method with clear examples.

Why Read Files Line by Line?

Imagine you have a very large log file. It could be several gigabytes in size. Loading the entire file into memory at once can crash your program. This is where line-by-line reading shines.

It processes one line at a time. Your program uses minimal memory. This technique is essential for data analysis, log parsing, and configuration file reading. It is a fundamental concept for any Python developer.

Method 1: Using the readline() Method

The readline() method reads a single line from a file. Each call gets the next line. It returns an empty string when it reaches the end of the file (EOF).

This method gives you fine-grained control. You can decide when to stop reading. It is useful for reading specific sections of a file.


# Open the file in read mode
file = open('data.txt', 'r')

# Read the first line
line1 = file.readline()
print(f"First line: {line1}")

# Read the second line
line2 = file.readline()
print(f"Second line: {line2}")

# Always close the file to free resources
file.close()
    

First line: This is the first line of the file.

Second line: Here is the second line.
    

Remember to close the file using close(). Forgetting to do this can lead to resource leaks. A better way is to use a context manager, which we will discuss later.

Method 2: Looping Over a File Object

The most common and Pythonic way is to use a for loop. You can loop directly over the file object. Python handles reading each line automatically.

This method is clean, readable, and memory-efficient. It is the recommended approach for most tasks.


# Using a for loop to iterate over each line
file = open('data.txt', 'r')
for line in file:
    # The line variable contains each line, including the newline character
    print(f"Line: {line.strip()}")  # .strip() removes leading/trailing whitespace
file.close()
    

Line: This is the first line of the file.
Line: Here is the second line.
Line: This is the end of the file.
    

The loop continues until the end of the file. You don't need to check for an EOF condition manually. This makes your code simpler and less error-prone.

Method 3: Using readlines() with Caution

The readlines() method reads all lines at once. It returns a list where each element is a line from the file.

Use this method only for small files. For large files, it will consume a lot of memory. You can then loop through this list.


file = open('data.txt', 'r')
all_lines = file.readlines()  # Reads EVERY line into memory
file.close()

for line in all_lines:
    print(f"List item: {line.strip()}")
    

While convenient, this approach defeats the purpose of memory-efficient reading. Stick to the for loop method for large datasets.

The Best Practice: Using 'with' Statement

The previous examples manually opened and closed files. The best practice is to use a with statement. This is called a context manager.

It automatically closes the file when the block is exited. This happens even if an error occurs. It prevents resource leaks and makes code cleaner.


# Using the 'with' statement for safe file handling
with open('data.txt', 'r') as file:
    for line_number, line in enumerate(file, start=1):
        print(f"Line {line_number}: {line.strip()}")
# File is automatically closed here
    

Line 1: This is the first line of the file.
Line 2: Here is the second line.
Line 3: This is the end of the file.
    

This is the most recommended method. It combines safety, readability, and efficiency. You should use this pattern in all your file-reading code. Once you've mastered reading files, you might want to learn How to Run Python File in Terminal to execute your scripts.

Handling File Paths and Errors

What if the file doesn't exist? Your program will crash with a FileNotFoundError. You should handle this gracefully using a try-except block.


filename = 'missing_file.txt'
try:
    with open(filename, 'r') as file:
        for line in file:
            print(line.strip())
except FileNotFoundError:
    print(f"Error: The file '{filename}' was not found.")
except IOError as e:
    print(f"An I/O error occurred: {e}")
    

Always use clear error messages. This helps you debug your code. It also provides a better experience if someone else uses your program.

Practical Example: Processing a Log File

Let's apply this knowledge to a real task. We will read a server log file. We will count how many times the word "ERROR" appears.


error_count = 0
log_file_path = 'server.log'

try:
    with open(log_file_path, 'r') as log_file:
        for line in log_file:
            if 'ERROR' in line.upper():  # Check for 'ERROR' case-insensitively
                error_count += 1
                print(f"Found error on line: {line.strip()}")
    print(f"\nTotal errors found: {error_count}")
except FileNotFoundError:
    print(f"Log file '{log_file_path}' not found.")
    

This example shows the power of line-by-line reading. You can process massive log files without slowing down your computer. The skills you learn here are foundational. For instance, knowing how to read files is crucial before you can How to Run Python File in Terminal effectively for automation tasks.

Conclusion

Reading a file line by line in Python is simple but powerful. The key method is using a for loop inside a with statement. This approach is safe, efficient, and Pythonic.

Avoid readlines() for large files. Always handle potential errors like missing files. Use these techniques to work with data logs, configuration files, and datasets of any size.

Mastering file I/O is a critical step in your Python journey. Practice with different types of files. Soon, you will handle file data with confidence. Remember, the next step is often to How to Run Python File in Terminal to put your file-reading scripts into action.