Question
How can I read every line of a file in Python and store each line as an element in a list?
I want to process the file line by line and append each line to the end of a list.
Short Answer
By the end of this page, you will understand how to read a text file in Python so that each line becomes one item in a list. You will learn the most common syntax, how newline characters behave, when to use readlines() versus looping over a file, and how this pattern appears in real Python code.
Concept
In Python, a file object is iterable, which means you can loop over it one line at a time using a for loop. This is one of the most common ways to read text files.
When you read a file line by line, each line is returned as a string. If you store those strings in a list, the result is a list where each element represents one line from the file.
This matters because many real programs work with line-based data:
- log files
- configuration files
- CSV-like plain text data
- lists of usernames, URLs, or IDs
- imported data for scripts
Python gives you a few ways to do this:
- iterate through the file and append each line to a list
- use
file.readlines()to get all lines at once - use a list comprehension for a compact version
The best choice depends on clarity, file size, and whether you want to clean each line as you read it.
A very important detail: lines often include a trailing newline character like \n. If you do not want that, you usually remove it with strip() or rstrip().
Mental Model
Think of a text file like a stack of paper forms.
- The file is the whole stack.
- Each line is one form.
- Your Python loop picks up one form at a time.
- The list is a tray where you place each form after reading it.
If the forms have an extra blank margin at the end, that is like the newline character \n. You can keep it, or trim it off before placing the form into the tray.
Syntax and Examples
The most common beginner-friendly approach is to use with open(...) and loop over the file.
lines = []
with open("data.txt", "r") as file:
for line in file:
lines.append(line)
print(lines)
What this does
open("data.txt", "r")opens the file for readingwithensures the file is closed automaticallyfor line in filereads one line at a timelines.append(line)adds each line to the list
Removing newline characters
Often you do not want each string to end with \n.
lines = []
with open("data.txt", "r") as file:
for line in file:
lines.append(line.strip())
print(lines)
If data.txt contains:
Step by Step Execution
Consider this file content:
cat
dog
bird
And this code:
lines = []
with open("animals.txt", "r") as file:
for line in file:
lines.append(line.strip())
print(lines)
Step by step
-
lines = []- An empty list is created.
-
with open("animals.txt", "r") as file:- Python opens the file in read mode.
filenow refers to the opened file object.
-
for line in file:- Python starts reading the file one line at a time.
-
First loop iteration:
lineis'cat\n'line.strip()becomes'cat'
Real World Use Cases
Reading a file line by line into a list is useful in many practical situations.
1. Loading a list of usernames
with open("users.txt") as file:
users = [line.strip() for line in file]
2. Reading configuration values
with open("hosts.txt") as file:
hosts = [line.strip() for line in file if line.strip()]
This skips empty lines.
3. Processing logs
with open("app.log") as file:
log_lines = file.readlines()
Later, your program can search for errors or warnings.
4. Importing plain-text data for scripts
with open("urls.txt") as file:
urls = [line.strip() for line in file]
This is common in automation and scraping scripts.
Real Codebase Usage
In real projects, developers usually combine file reading with cleanup and validation.
Common patterns
Simple line loading
with open("items.txt") as file:
items = [line.strip() for line in file]
Skip empty lines
with open("items.txt") as file:
items = [line.strip() for line in file if line.strip()]
Validate values while reading
valid_ids = []
with open("ids.txt") as file:
for line in file:
value = line.strip()
if not value:
continue
if value.isdigit():
valid_ids.append(int(value))
Guard clauses for missing files
from pathlib Path
path = Path()
path.exists():
()
:
path.() file:
lines = [line.strip() line file]
Common Mistakes
Beginners often run into a few common issues.
1. Forgetting to use with
Broken style:
file = open("data.txt", "r")
lines = file.readlines()
This works, but the file may not be closed properly if something goes wrong.
Better:
with open("data.txt", "r") as file:
lines = file.readlines()
2. Being surprised by \n
Broken expectation:
with open("data.txt") as file:
lines = file.readlines()
print(lines)
# Might print: ['apple\n', 'banana\n']
Fix:
with open("data.txt") as file:
lines = [line.strip() for line in file]
3. Using when only the newline should be removed
Comparisons
| Approach | Example | Best for | Notes |
|---|---|---|---|
Loop + append() | for line in file: lines.append(line) | Learning and custom processing | Clear and flexible |
readlines() | lines = file.readlines() | Quick full-file loading | Simple, but reads all lines at once |
| List comprehension | lines = [line.strip() for line in file] | Clean, concise code | Common in real projects |
read() | text = file.read() | Reading the whole file as one string | Not line-based |
Cheat Sheet
# Read all lines into a list
with open("data.txt", "r") as file:
lines = file.readlines()
# Read lines and remove newlines
with open("data.txt", "r") as file:
lines = [line.strip() for line in file]
# Preserve leading spaces, remove only newline
with open("data.txt", "r") as file:
lines = [line.rstrip("\n") for line in file]
# Manual append style
lines = []
with open("data.txt", "r") as file:
for line in file:
lines.append(line)
Key rules
- Use
with open(...)to close files automatically. - A file object can be looped over line by line.
readlines()returns a list of strings.- Each line may contain
\nat the end. - Use
strip()or if needed.
FAQ
Why does each line contain \n in Python?
Because text lines in files usually end with a newline character. Python includes that character when reading the line.
What is the safest way to open a file in Python?
Use with open(...) as file: because it closes the file automatically, even if an error happens.
Should I use readlines() or a for loop?
Use readlines() for quick simple cases. Use a for loop when you want more control or when working with larger files.
How do I remove blank lines while reading a file?
Use a condition in a comprehension:
with open("data.txt") as file:
lines = [line.strip() for line in file if line.strip()]
Can I store file lines directly into a list without calling append()?
Yes. A list comprehension is a common way:
with open("data.txt") file:
lines = [line.strip() line file]
Mini Project
Description
Build a small Python script that reads a text file of tasks and stores each task as an item in a list. This demonstrates how to read line by line, remove newline characters, and ignore empty lines, which is a common pattern in scripts and small utilities.
Goal
Create a program that loads tasks from a file into a clean Python list and prints the numbered results.
Requirements
- Open a text file named
tasks.txtfor reading. - Read the file line by line.
- Store each non-empty line in a list.
- Remove newline characters from each line.
- Print the final list as a numbered task list.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.