Python
File Handling
Programming
Coding Tutorial
Read File

How to read a file line-by-line into a list?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Reading a file line-by-line into a list is a common operation in programming, especially when dealing with text files, configuration files, or logs. This technique is valuable for parsing data, processing file content, and efficiently loading data for further analysis. This article will explore different methods for reading a file line-by-line into a list in Python, providing technical explanations and examples.

Method 1: Using open() and readlines()

The simplest method to read a file line-by-line into a list is by using the built-in open() function in conjunction with the readlines() method. Here's how it works:

python
1# Open the file in read mode
2with open('example.txt', 'r') as file:
3    # Read all lines and store them in a list
4    lines = file.readlines()

Explanation

  • open('example.txt', 'r'): Opens the file 'example.txt' in read mode ('r'). The with statement ensures the file is properly closed after its block is executed.
  • readlines(): Reads all lines in the file and returns a list where each element corresponds to a line in the file.

Pros and Cons

AdvantagesDisadvantages
Simple to useLoads entire file into memory
Few lines of codeNot suitable for large files

Method 2: Using a for Loop

Sometimes, it's more efficient to read a file line-by-line using a for loop, which processes each line individually. This method is ideal for large files where memory usage is a concern.

python
1# Open the file in read mode
2with open('example.txt', 'r') as file:
3    # Initialize an empty list
4    lines = []
5    # Iterate over each line in the file
6    for line in file:
7        # Append each line to the list
8        lines.append(line.rstrip('\n'))

Explanation

  • By iterating over the file object, we read each line one at a time.
  • line.rstrip('\n'): Removes the newline character \n from the end of each line.

Pros and Cons

AdvantagesDisadvantages
Memory efficientSlightly more complex code
Suitable for large filesRequires manual line ending management

Method 3: Using List Comprehension

List comprehension offers an elegant and concise way to read a file into a list. This method is particularly useful for pre-processing lines.

python
# Open the file and read lines using list comprehension
with open('example.txt', 'r') as file:
    lines = [line.strip() for line in file]

Explanation

  • line.strip(): Removes surrounding whitespace, including newline characters. This results in cleaner data.
  • List comprehension reads and processes lines concisely.

Pros and Cons

AdvantagesDisadvantages
Concise, elegant syntaxStill loads entire file
Usually faster due to optimizationsNot as readable as a loop

Handling Large Files with fileinput module

For extremely large files, Python offers the fileinput module, which processes files line-by-line without the need to load them entirely into memory.

python
1import fileinput
2
3lines = []
4for line in fileinput.input(files=('example.txt',)):
5    lines.append(line.strip())

Explanation

  • fileinput.input(): Can process multiple files in sequence as if they were one single file, useful for batch processing.
  • Efficient for very large files or a list of files.

Pros and Cons

AdvantagesDisadvantages
Handles large/distributed files seamlesslyRequires an additional import
Supports multiple files simultaneouslySlightly more opaque syntax

Additional Subtopics

Exception Handling

When reading files, exception handling is crucial to manage errors like missing files or read permission issues.

python
1try:
2    with open('example.txt', 'r') as file:
3        lines = file.readlines()
4except FileNotFoundError:
5    print("The file was not found.")
6except IOError:
7    print("An I/O error occurred.")

Using Path from pathlib

For more sophisticated path manipulation, especially helpful on different operating systems, use pathlib.

python
1from pathlib import Path
2
3# Specify the file path
4file_path = Path('example.txt')
5
6# Read lines using Path
7with file_path.open('r') as file:
8    lines = file.readlines()

Closing Thoughts and Best Practice

When working with file I/O, consider the nature and size of the data to choose the most efficient method. For small files, readlines() is straightforward, but for large files, a for loop or the fileinput module is more appropriate. Always incorporate exception handling to make your code robust and handle unexpected scenarios gracefully.

By leveraging these approaches, you can read files efficiently, aiding in data analysis, file manipulation, and automating tasks.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.