- Understand how to open and read files safely
- Use the with statement for automatic resource management
- Read files line by line and in bulk
- Handle different file encodings
Reading Files
Imagine your program is like a person with amnesia – every time it stops running, it forgets everything! Files are the solution: they're like notebooks where your program can write things down and read them later. Files let your programs remember data, share information, and work with external content.
In this lesson, you'll learn how to read files – the foundation of working with persistent data. Whether it's configuration settings, user data, or text documents, reading files is an essential skill.
What is a File?
A file is a container for storing data on your computer:
Types of Files
TEXT FILES: Human-readable content
.txt - Plain text
.csv - Comma-separated values
.json - JavaScript Object Notation
.py - Python source code
.html - Web pages
BINARY FILES: Machine-readable content
.jpg, .png - Images
.mp3, .wav - Audio
.pdf - Documents
.exe - Programs
In this lesson: TEXT FILES (most common for beginners)
Opening Files: The Gateway
Before reading a file, you must open it – like opening a book before reading:
The open() Function
# Basic syntax
file = open("filename.txt", "mode")
# Example
file = open("hello.txt", "r") # Open for reading
content = file.read() # Read the content
file.close() # IMPORTANT: Close when done!
File Opening Modes
| Mode | Description | File Must Exist? |
|---|---|---|
'r' |
Read (default) - read only | Yes |
'w' |
Write - creates/overwrites | No (creates it) |
'a' |
Append - adds to end | No (creates it) |
'r+' |
Read+Write | Yes |
'rb' |
Read Binary | Yes |
The with Statement: The Safe Way
Always use with when working with files!
Why Use 'with'?
WITHOUT 'with' (risky):
file = open("data.txt", "r")
content = file.read()
# What if an error happens here?
# The file might stay open forever!
file.close() # Might never reach this line
WITH 'with' (safe):
with open("data.txt", "r") as file:
content = file.read()
# File automatically closes here, even if error occurs!
Think of 'with' as a responsible librarian who always
puts the book back on the shelf when you're done!
Basic Reading Pattern
# The standard pattern you'll use 99% of the time
with open("my_file.txt", "r") as file:
content = file.read()
print(content)
# After the 'with' block, file is automatically closed
# No need to call file.close()!
Reading Methods
Python offers several ways to read file content:
1. read() - Read Everything
with open("poem.txt", "r") as file:
content = file.read() # Entire file as one string
print(content)
Best for: Small files that fit in memory
2. readline() - Read One Line
with open("poem.txt", "r") as file:
first_line = file.readline() # Gets first line
second_line = file.readline() # Gets second line
print(first_line)
print(second_line)
Best for: When you only need specific lines
3. readlines() - Read All Lines as List
with open("poem.txt", "r") as file:
lines = file.readlines() # List of all lines
print(lines)
# ['First line\n', 'Second line\n', 'Third line\n']
Best for: When you need to work with lines as a list
4. Iterate Line by Line (Memory Efficient!)
with open("big_file.txt", "r") as file:
for line in file: # One line at a time
print(line.strip()) # strip() removes \n
Best for: Large files (doesn't load entire file into memory)
Comparison
Reading Methods Comparison
METHOD RETURNS MEMORY USE WHEN
read() One big string High Small files
readline() One line Low Few lines needed
readlines() List of lines High Need line list
for line in f One line/iter Low Large files
WINNER for most cases: for line in file
Memory efficient and simple!
Handling Encodings
Text files use different character encodings (ways to represent letters):
# UTF-8 is the modern standard (handles all languages)
with open("french.txt", "r", encoding="utf-8") as file:
content = file.read()
# On Windows, might need to specify encoding explicitly
with open("data.txt", "r", encoding="utf-8") as file:
content = file.read()
Common Encodings
| Encoding | Description | When to Use |
|---|---|---|
utf-8 |
Universal | Modern standard |
latin-1 |
Western European | Legacy files |
cp1252 |
Windows Western | Old Windows files |
ascii |
Basic English only | Simple text |
Pro tip: Always specify encoding="utf-8" – it prevents most encoding errors!
Practical Examples
Example 1: Reading a Configuration File
def load_config(filename):
"""Load key-value configuration from a file."""
config = {}
with open(filename, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith("#"):
continue
# Parse key=value format
if "=" in line:
key, value = line.split("=", 1)
config[key.strip()] = value.strip()
return config
# Example config.txt:
# # This is a comment
# username=alice
# theme=dark
# language=en
config = load_config("config.txt")
print(config) # {'username': 'alice', 'theme': 'dark', 'language': 'en'}
Example 2: Counting Lines and Words
def analyze_file(filename):
"""Count lines, words, and characters in a file."""
lines = 0
words = 0
chars = 0
with open(filename, "r", encoding="utf-8") as file:
for line in file:
lines += 1
words += len(line.split())
chars += len(line)
return {
"lines": lines,
"words": words,
"characters": chars
}
stats = analyze_file("document.txt")
print(f"Lines: {stats['lines']}")
print(f"Words: {stats['words']}")
print(f"Characters: {stats['characters']}")
Example 3: Finding Text in a File
def search_file(filename, search_term):
"""Find all lines containing a search term."""
results = []
with open(filename, "r", encoding="utf-8") as file:
for line_num, line in enumerate(file, 1):
if search_term.lower() in line.lower():
results.append({
"line_number": line_num,
"content": line.strip()
})
return results
# Find all lines containing "error"
matches = search_file("log.txt", "error")
for match in matches:
print(f"Line {match['line_number']}: {match['content']}")
Common Mistakes
1. Forgetting to Close Files
# BAD: File might stay open
file = open("data.txt", "r")
content = file.read()
# Forgot file.close()!
# GOOD: Use 'with' statement
with open("data.txt", "r") as file:
content = file.read()
# Automatically closed!
2. File Not Found
# File doesn't exist - raises FileNotFoundError
with open("nonexistent.txt", "r") as file:
content = file.read()
# Handle the error gracefully
try:
with open("nonexistent.txt", "r") as file:
content = file.read()
except FileNotFoundError:
print("File not found!")
content = ""
3. Encoding Errors
# Default encoding might fail on special characters
with open("french.txt", "r") as file: # Might crash!
content = file.read()
# Always specify encoding
with open("french.txt", "r", encoding="utf-8") as file:
content = file.read()
Key Takeaways
Remember These Points
Always use 'with' to open files
with open("file.txt", "r") as f:
Choose the right reading method:
• read() for small files
• for line in file for large files
Specify encoding (especially utf-8)
encoding="utf-8"
Handle FileNotFoundError for safety
Use strip() to remove newline characters
line.strip()
What's Next?
You've learned to read files! But what about saving data? In the next lesson, we'll explore writing files – how to create new files and save your program's output for later use.
