Reading Files
File handling is a fundamental skill for any programmer. Whether you're processing
configuration files, reading data for analysis, or loading user content - Python makes file
operations simple and intuitive. The built-in open() function is your gateway to
reading files, and understanding the different modes and methods will make you confident working
with any file type!
File Open Modes
The open() function takes a filename and a mode parameter. The mode determines
how the file is opened: for reading, writing, appending, or creating. You can also specify
text or binary mode. Understanding these modes is essential for working with files correctly.
Click Run to execute your code
'r' - Read (default, file must exist)'w' - Write (creates new or truncates)'a' - Append (creates new or adds to end)'x' - Exclusive create (fails if exists)'b' - Binary mode (add to others: 'rb', 'wb')'+' - Read and write (add to others: 'r+', 'w+')
Reading Methods
Once a file is opened, you have several methods to read its content: read() gets
everything or a specific number of characters, readline() reads one line at a time,
and readlines() returns all lines as a list. Choose the right method based on your
needs and file size.
Click Run to execute your code
read() - Small files you need entirely in memoryread(n) - When you need specific character countsreadline() - Processing one line at a timereadlines() - When you need random access to linesfor line in file - Large files (most memory efficient!)
Iterating Through Files
File objects are iterable - you can loop through them directly with for. This is
the most memory-efficient way to read large files because Python only loads one line at a time.
Combined with the with statement, this is the recommended way to read files.
Click Run to execute your code
open(),
always call f.close() when done, or better yet, use the with
statement which automatically closes the file. Unclosed files can lead to data loss,
resource leaks, and file locking issues.
Reading Binary Files
Binary mode ('rb') reads files as raw bytes instead of text. This is essential
for images, executables, compressed files, and any non-text data. Binary reading returns
bytes objects instead of strings, and you can use seek() and
tell() to navigate within the file.
Click Run to execute your code
Common Mistakes
1. Forgetting to close files
# Wrong - file stays open!
f = open("data.txt", "r")
content = f.read()
# Forgot f.close()!
# Correct - always close
f = open("data.txt", "r")
content = f.read()
f.close()
# Best - use 'with' statement
with open("data.txt", "r") as f:
content = f.read()
# Automatically closed!
2. Reading file twice without seeking
# Wrong - second read returns empty!
f = open("data.txt", "r")
content1 = f.read() # Reads all
content2 = f.read() # Returns '' - cursor at end!
f.close()
# Correct - seek back to start
f = open("data.txt", "r")
content1 = f.read()
f.seek(0) # Go back to beginning
content2 = f.read() # Now it works!
f.close()
3. Not handling FileNotFoundError
# Wrong - crashes if file doesn't exist
f = open("nonexistent.txt", "r")
# Correct - handle the error
try:
f = open("nonexistent.txt", "r")
content = f.read()
f.close()
except FileNotFoundError:
print("File not found!")
content = ""
# Or check first
import os
if os.path.exists("file.txt"):
with open("file.txt", "r") as f:
content = f.read()
4. Wrong mode for binary files
# Wrong - reading image as text
with open("image.png", "r") as f:
data = f.read() # UnicodeDecodeError!
# Correct - use binary mode
with open("image.png", "rb") as f:
data = f.read() # Returns bytes
5. Reading entire large file into memory
# Wrong - loads 10GB file into memory!
with open("huge_file.txt", "r") as f:
all_lines = f.readlines() # Memory error!
# Correct - iterate line by line
with open("huge_file.txt", "r") as f:
for line in f: # Only one line in memory
process(line)
# Or read in chunks
with open("huge_file.bin", "rb") as f:
while chunk := f.read(8192): # 8KB chunks
process(chunk)
Exercise: Word Counter
Task: Read a file and count words, lines, and characters.
Requirements:
- Open a file and read its contents
- Count the number of lines
- Count the number of words
- Count the number of characters (excluding newlines)
Click Run to execute your code
Show Solution
# Create a sample file first
with open("sample.txt", "w") as f:
f.write("Hello World\n")
f.write("Python is awesome\n")
f.write("File handling is easy")
def count_file_stats(filename):
"""Count lines, words, and characters in a file."""
with open(filename, "r") as f:
content = f.read()
lines = content.split('\n')
line_count = len(lines)
words = content.split()
word_count = len(words)
# Characters excluding newlines
char_count = len(content.replace('\n', ''))
return line_count, word_count, char_count
# Test it
lines, words, chars = count_file_stats("sample.txt")
print(f"Lines: {lines}")
print(f"Words: {words}")
print(f"Characters: {chars}")
# Cleanup
import os
os.remove("sample.txt")
Summary
- Open file:
f = open("file.txt", "r") - Read all:
content = f.read() - Read n chars:
partial = f.read(100) - Read line:
line = f.readline() - Read all lines:
lines = f.readlines() - Iterate:
for line in f:(most efficient) - Binary mode:
open("file", "rb") - File position:
f.tell(),f.seek(0) - Always use:
with open(...) as f:for auto-close - Handle errors:
try/except FileNotFoundError
What's Next?
Now that you can read files, let's learn how to write files - creating new files, writing content, and appending data. Writing is just as important as reading for saving results, creating logs, and generating output!
Enjoying these tutorials?