How to read a file line-by-line into a list?
Categories:
How to Read a File Line-by-Line into a List in Python

Learn various Python techniques to efficiently read the contents of a text file, line by line, and store each line as an element in a list.
Reading files is a fundamental operation in many programming tasks. When dealing with text files, it's often necessary to process their content line by line. Python provides several straightforward and efficient ways to achieve this, allowing you to easily load each line of a file into a list for further manipulation. This article will explore the most common and Pythonic methods, along with their advantages and considerations.
The Basic Approach: Using readlines()
The most direct way to read all lines from a file into a list is by using the readlines()
method of a file object. This method reads all lines from the file and returns them as a list of strings, where each string represents a line, including the newline character \n
at the end.
with open('my_file.txt', 'r') as f:
lines = f.readlines()
print(lines)
Using readlines()
to read all lines into a list.
with
statement when opening files. It ensures that the file is properly closed even if errors occur, preventing resource leaks.Iterating Over a File Object (More Memory Efficient)
While readlines()
is simple, it reads the entire file into memory at once. For very large files, this can be inefficient or even lead to MemoryError
. A more memory-efficient approach is to iterate directly over the file object. When you iterate over a file object, it yields one line at a time, allowing you to process it without loading the entire file into memory. You can then append each line to a list.
lines = []
with open('my_file.txt', 'r') as f:
for line in f:
lines.append(line)
print(lines)
Iterating over a file object to read lines.
readlines()
and iterating over the file object will include the newline character \n
at the end of each line. You'll often want to remove this using line.strip()
or line.rstrip('\n')
.Removing Newline Characters and Empty Lines
Typically, when reading lines into a list, you'll want to clean up the data by removing the trailing newline characters and potentially filtering out any empty lines. The strip()
method is perfect for this, as it removes leading/trailing whitespace, including newlines.
# Using a list comprehension for conciseness and efficiency
with open('my_file.txt', 'r') as f:
lines = [line.strip() for line in f]
# To also filter out empty lines
with open('my_file.txt', 'r') as f:
non_empty_lines = [line.strip() for line in f if line.strip()]
print("All lines (stripped):", lines)
print("Non-empty lines (stripped):", non_empty_lines)
Reading lines, stripping whitespace, and filtering empty lines using list comprehensions.
flowchart TD A[Start] --> B{Open File 'my_file.txt'} B --> C{Read Mode 'r'} C --> D{File Object 'f'} D -- Iterate line by line --> E{Process Line} E --> F{Strip Newline/Whitespace} F -- Append to List --> G[List of Lines] G --> H{Close File (via 'with')} H --> I[End]
Flowchart of reading a file line-by-line into a list.
encoding
parameter when opening the file, e.g., open('my_file.txt', 'r', encoding='latin-1')
.