ZetCode

Python readline Function

Last modified March 26, 2025

This comprehensive guide explores Python's readline function, the primary method for reading files line by line in Python. We'll cover basic usage, file handling, context managers, and best practices. Through practical examples, you'll master line-by-line file reading in Python.

Basic Definitions

The readline function reads a single line from a file each time it is called. It returns the line as a string including the newline character at the end. When the end of file is reached, it returns an empty string.

Key characteristics of readline: it maintains file position between calls, is memory efficient for large files, and preserves newline characters. It's ideal for processing files that don't fit in memory or need line-by-line processing.

Basic readline Usage

The simplest use of readline reads one line at a time from a file. Each call advances the file position to the next line.

basic_readline.py
# Open a file for reading
file = open('example.txt', 'r')

# Read first line
line1 = file.readline()
print("First line:", line1)

# Read second line
line2 = file.readline()
print("Second line:", line2)

file.close()

This example opens 'example.txt' in read mode ('r'), reads the first line with readline, then reads the second line. Each call to readline returns the next line in the file. The newline character at the end of each line is included in the returned string.

Remember to close the file with close when done. The file position advances automatically with each readline call, tracking where the next read should start.

Reading Entire File with readline

You can read an entire file line by line using a loop with readline. This is memory efficient for large files.

readline_loop.py
# Read entire file line by line
file = open('example.txt', 'r')

while True:
    line = file.readline()
    if not line:  # Empty string means EOF
        break
    print(line.strip())  # Remove newline and print

file.close()

This code reads 'example.txt' line by line until readline returns an empty string, indicating end of file. The strip method removes whitespace and newline characters from each line before printing.

This approach is particularly useful for very large files that shouldn't be loaded entirely into memory. It processes one line at a time, keeping memory usage constant regardless of file size.

Using readline with Context Managers

The with statement creates a context manager that automatically closes files. This is the recommended way to use readline.

readline_context.py
# Using readline with context manager
with open('example.txt', 'r') as file:
    line = file.readline()
    while line:
        print(line.strip())
        line = file.readline()

This example demonstrates the preferred way to handle files in Python. The with block ensures the file is properly closed after processing. The loop continues until readline returns an empty string.

Context managers prevent resource leaks and make code cleaner by eliminating explicit close calls. They work even if exceptions occur within the block.

Controlling Line Reading with Size

readline accepts an optional size parameter to limit how many characters to read from the line. This can be useful for fixed-width formats.

readline_size.py
# Using readline with size parameter
with open('data.txt', 'r') as file:
    # Read first 10 characters of first line
    part1 = file.readline(10)
    print("First 10 chars:", part1)
    
    # Read next 5 characters (continues same line)
    part2 = file.readline(5)
    print("Next 5 chars:", part2)
    
    # Read rest of line
    rest = file.readline()
    print("Rest of line:", rest)

This code demonstrates how the size parameter limits character reading. The first call reads 10 characters, the next reads 5 more from the same line, and the final call reads the remainder of the line. The file position advances only within the current line until a full line is read.

When using the size parameter, readline will return when either the specified number of characters is read or a newline is encountered, whichever comes first.

Comparing readline and Iteration

File objects are iterable in Python, providing an alternative to readline for line-by-line reading. Both approaches have their uses.

readline_vs_iter.py
# Comparing readline and file iteration
with open('example.txt', 'r') as file:
    print("Using readline:")
    line = file.readline()
    while line:
        print(line.strip())
        line = file.readline()
    
    file.seek(0)  # Rewind file
    
    print("\nUsing iteration:")
    for line in file:
        print(line.strip())

This example shows both methods for reading lines. The readline approach offers more control (like size limits), while iteration is more concise. Both methods preserve memory efficiency for large files.

The iteration approach is generally preferred for simple line-by-line reading, while readline is better when you need precise control or want to read partial lines.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.