Reading Files in Python: open(), readlines(), and Context Managers
Learn to read files from disk using open(), readlines(), and context managers. Read your first CSV file.
Everything you've built so far lived inside Python. Hardcoded names. Made-up numbers. Today we go outside. Your CSV is sitting right there on disk. Python can't see it yet. Let's fix that.
The Problem with Manual File Handling
In Week 1, you wrote code like this:
sales = [
{"name": "Alice Chen", "amount": 1250.50, "region": "West", "status": "confirmed"},
{"name": "Bob Kumar", "amount": 340.50, "region": "East", "status": "pending"},
# ... more hardcoded data
]
This works, but it doesn't scale. Real data lives in files. CSV files. JSON files. Database dumps. Today, we read from disk.
So we just... read the file? How?
Python gives you open(). It's built-in. No imports needed.
f = open('sales.csv')
lines = f.readlines()
f.close()
That works, but there's a problem. If your code crashes before f.close(), the file stays open. Memory leaks. Resource exhaustion. Bad vibes.
Maya(thinking): Yeah, that seems fragile. There's a better way?
Absolutely. The with statement.
Enter Context Managers: with open()
with open('sales.csv') as f:
lines = f.readlines()
print(lines)
# The file is *automatically* closed here
# Even if something crashed inside the block, f.close() still runs
The with keyword is admittedly weird-looking. But it's doing something powerful: it guarantees cleanup. No matter what happens inside the block, the file closes.
"with"? That's... a lot of indentation for file handling.
I know. It looks strange at first. But with is everywhere in Python. Context managers are a superpower. Files, database connections, locks—anything that needs cleanup uses with.
Reading a CSV File Line by Line
Now let's read the sales.csv file that's sitting in your project:
def read_csv_lines(filepath):
with open(filepath) as f:
return f.readlines()
lines = read_csv_lines('sales.csv')
for line in lines:
print(line)
Let's trace through:
open('sales.csv')opens the file in read mode (default).readlines()reads all lines at once, returns a list of strings- Each string includes the newline character at the end (
\n) withblock exits → file automatically closes
Maya(thinking): Wait. If readlines() reads all lines, what about huge files? Millions of rows?
Great question. For now, .readlines() is fine for files under 100MB. For massive files, we loop with .readline() (singular) and process one line at a time. Week 5, maybe. For today, load it all.
Maya(excited): And these are raw strings, right? So
'Alice Chen,1250.50,West,confirmed\n'?
Exactly. Still strings. Still have the \n. Tomorrow's lesson, we split them into fields. For now, you've got the lines. That's progress.
Your Challenge
You'll write read_csv_lines(filepath). It should:
- Open the file with
with open(filepath) as f: - Call
.readlines()to get all lines - Return the list
Test it on sales.csv. You should get 7 lines (header + 6 data rows). Some rows are messy (Eve Williams has extra spaces, the last row has missing data). That's real-world CSV. We'll handle it next lesson.
Next time: Parsing. Right now your lines are just strings. Tomorrow we split them with .split(',') and turn them into dictionaries. The messy data gets real interesting then.
Practice your skills
Sign up to write and run code in this lesson.
Reading Files in Python: open(), readlines(), and Context Managers
Learn to read files from disk using open(), readlines(), and context managers. Read your first CSV file.
Everything you've built so far lived inside Python. Hardcoded names. Made-up numbers. Today we go outside. Your CSV is sitting right there on disk. Python can't see it yet. Let's fix that.
The Problem with Manual File Handling
In Week 1, you wrote code like this:
sales = [
{"name": "Alice Chen", "amount": 1250.50, "region": "West", "status": "confirmed"},
{"name": "Bob Kumar", "amount": 340.50, "region": "East", "status": "pending"},
# ... more hardcoded data
]
This works, but it doesn't scale. Real data lives in files. CSV files. JSON files. Database dumps. Today, we read from disk.
So we just... read the file? How?
Python gives you open(). It's built-in. No imports needed.
f = open('sales.csv')
lines = f.readlines()
f.close()
That works, but there's a problem. If your code crashes before f.close(), the file stays open. Memory leaks. Resource exhaustion. Bad vibes.
Maya(thinking): Yeah, that seems fragile. There's a better way?
Absolutely. The with statement.
Enter Context Managers: with open()
with open('sales.csv') as f:
lines = f.readlines()
print(lines)
# The file is *automatically* closed here
# Even if something crashed inside the block, f.close() still runs
The with keyword is admittedly weird-looking. But it's doing something powerful: it guarantees cleanup. No matter what happens inside the block, the file closes.
"with"? That's... a lot of indentation for file handling.
I know. It looks strange at first. But with is everywhere in Python. Context managers are a superpower. Files, database connections, locks—anything that needs cleanup uses with.
Reading a CSV File Line by Line
Now let's read the sales.csv file that's sitting in your project:
def read_csv_lines(filepath):
with open(filepath) as f:
return f.readlines()
lines = read_csv_lines('sales.csv')
for line in lines:
print(line)
Let's trace through:
open('sales.csv')opens the file in read mode (default).readlines()reads all lines at once, returns a list of strings- Each string includes the newline character at the end (
\n) withblock exits → file automatically closes
Maya(thinking): Wait. If readlines() reads all lines, what about huge files? Millions of rows?
Great question. For now, .readlines() is fine for files under 100MB. For massive files, we loop with .readline() (singular) and process one line at a time. Week 5, maybe. For today, load it all.
Maya(excited): And these are raw strings, right? So
'Alice Chen,1250.50,West,confirmed\n'?
Exactly. Still strings. Still have the \n. Tomorrow's lesson, we split them into fields. For now, you've got the lines. That's progress.
Your Challenge
You'll write read_csv_lines(filepath). It should:
- Open the file with
with open(filepath) as f: - Call
.readlines()to get all lines - Return the list
Test it on sales.csv. You should get 7 lines (header + 6 data rows). Some rows are messy (Eve Williams has extra spaces, the last row has missing data). That's real-world CSV. We'll handle it next lesson.
Next time: Parsing. Right now your lines are just strings. Tomorrow we split them with .split(',') and turn them into dictionaries. The messy data gets real interesting then.