File Operations
Chapter 6: File Operations
File operations let programs persist data beyond runtime, reading from and writing to files on disk. Instead of losing data when a program ends, you save it to files for later use. Python makes file handling straightforward with built-in functions and context managers that ensure safe, efficient file access.
This chapter covers standard Python file operations as they work in desktop and server environments. Note that browser/WASM environments like Pyodide have limited file system access—these examples demonstrate core concepts you'll use in local Python development, data processing scripts, and server applications.
Reading Files with open()
The open() function opens files, returning a file object for reading or writing. The simplest use reads an entire file:
The first argument is the file path, the second is the mode. Mode 'r' opens for reading (default mode). The read() method returns the entire file contents as a string.
Important: Always close files after use with file.close(). Unclosed files waste system resources and can cause data corruption.
The with Statement: Safe File Handling
Python's with statement automatically closes files, even if errors occur. This is the recommended approach for file operations:
The with statement creates a context manager. When the block ends, Python automatically calls file.close(), even if exceptions occur. This prevents resource leaks and ensures clean file handling.
Reading Line by Line
For large files, reading everything at once wastes memory. Read line by line instead:
with open('example.txt', 'r') as file:
for line in file:
print(line.strip()) # strip() removes trailing newlineThis approach is memory-efficient—Python reads one line at a time, processing it before loading the next.
Alternatively, use readlines() to get a list of all lines:
with open('example.txt', 'r') as file:
lines = file.readlines()
print(f"Total lines: {len(lines)}")
print(f"First line: {lines[0].strip()}")Writing Files
Write to files using mode 'w' (overwrite) or 'a' (append). Mode 'w' creates the file if it doesn't exist, or erases existing contents:
# Write mode: overwrites existing content
with open('output.txt', 'w') as file:
file.write("Hello, World!\n")
file.write("This is a new file.\n")The write() method doesn't add newlines automatically—include \n explicitly when needed.
Appending to Files
Mode 'a' appends to existing files without erasing previous content:
# Append mode: adds to existing content
with open('log.txt', 'a') as file:
file.write("New log entry\n")
file.write("Another entry\n")Append mode is perfect for logs, where you continuously add entries without losing previous data.
Writing Multiple Lines
Use writelines() to write a list of strings:
lines = [
"First line\n",
"Second line\n",
"Third line\n"
]
with open('multi.txt', 'w') as file:
file.writelines(lines)Remember: writelines() doesn't add newlines—include them in your strings.
File Modes Explained
Python supports several file modes:
'r'- Read (default). File must exist.'w'- Write. Creates file or erases existing content.'a'- Append. Creates file or adds to existing content.'r+'- Read and write. File must exist.'w+'- Write and read. Creates file or erases existing content.'a+'- Append and read. Creates file or adds to existing content.
For binary files (images, videos, etc.), add 'b': 'rb', 'wb', 'ab'. Binary mode handles files as bytes rather than text.
Working with File Paths
Use raw strings (prefix with r) for Windows paths to avoid backslash issues:
# Windows path with raw string
with open(r'C:\Users\Name\file.txt', 'r') as file:
content = file.read()Or use forward slashes—Python converts them automatically:
# Forward slashes work on all platforms
with open('C:/Users/Name/file.txt', 'r') as file:
content = file.read()For portable code, use the pathlib module (covered in advanced topics) or os.path.join() to build platform-independent paths.
Error Handling for Files
Files might not exist, or you might lack permissions. Handle errors gracefully:
try:
with open('might_not_exist.txt', 'r') as file:
content = file.read()
print(content)
except FileNotFoundError:
print("File not found. Please check the path.")
except PermissionError:
print("Permission denied. Check file permissions.")Exception handling prevents crashes when file operations fail, providing better user experience.
Practical File Patterns
Reading Configuration:
# Read config file line by line
settings = {}
with open('config.txt', 'r') as file:
for line in file:
key, value = line.strip().split('=')
settings[key] = value
print(settings)Writing CSV Data:
# Simple CSV writing
data = [
["Name", "Age", "City"],
["Alice", "30", "NYC"],
["Bob", "25", "LA"]
]
with open('data.csv', 'w') as file:
for row in data:
file.write(','.join(row) + '\n')Logging Events:
from datetime import datetime
def log_event(message):
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with open('events.log', 'a') as file:
file.write(f"[{timestamp}] {message}\n")
log_event("Application started")
log_event("User logged in")WASM and Browser Considerations
The examples in this chapter work in standard Python environments (desktop, server). Browser-based Python (Pyodide/WASM) has limited file system access for security reasons. In web environments, you typically:
- Use browser APIs for file uploads/downloads
- Work with in-memory data structures
- Store data in browser local storage
- Communicate with servers for file operations
When learning file operations, test in a local Python environment. These fundamentals apply everywhere, even though implementation details vary by platform.
Try It Yourself: Practice Exercises
Exercise 1: Read and Count
Create a text file with several lines. Write a program that reads the file and prints the number of lines, words, and characters.
Exercise 2: File Copy
Write a program that reads one file and writes its contents to another file (a simple file copy program).
Exercise 3: Search and Replace
Read a file, replace all occurrences of a word with another word, and write the result to a new file.
Exercise 4: CSV Sum
Create a CSV file with numbers. Write a program that reads the file, sums the numbers in each column, and prints the totals.
Exercise 5: Log Analyzer
Create a log file with timestamped entries. Write a program that reads the log and counts how many entries contain specific keywords.
Exercise 6: Filter Lines
Read a file and write only lines containing a specific word to a new output file.
What's Next?
You've mastered Python file operations: reading and writing files, using context managers for safety, understanding file modes, and handling paths. These skills enable data persistence, configuration management, and processing external data in Python applications.
In the next chapter, we'll explore error handling comprehensively—understanding exceptions, using try-except blocks effectively, creating custom exceptions, and building robust error-resistant programs.
📝 Test Your Knowledge: File Operations
Take this quiz to reinforce what you've learned in this chapter.