🕯️ Magic Note
Python’s file handling is built on top of your operating system’s file system. When you open a file, Python asks the OS for permission. The OS checks if the file exists, if you have access, and then gives Python a file object (a “handle”) to work with. This is why file paths look different on Windows (using backslashes) versus Mac/Linux (using forward slashes).
Python
# Basic syntax
file = open(“filename.txt”, “r”)
# Do something with file
file.close()
| Mode | What It Does | File Must Exist | Creates File |
|---|---|---|---|
| “r” | Read only (default) | Yes | No |
| “w” | Write (overwrites existing content) | No | Yes |
| “a” | Append (adds to end of file) | No | Yes |
| “x” | Exclusive creation (fails if file exists) | No | Yes |
| “r+” | Read and write | Yes | No |
| “w+” | Write and read (overwrites) | No | Yes |
| “a+” | Append and read | No | Yes |
Python
# Different modes in action
# Read mode (file must exist)
file = open(“data.txt”, “r”)
# Write mode (creates new file or overwrites existing)
file = open(“output.txt”, “w”)
# Append mode (adds to end, creates if not exists)
file = open(“log.txt”, “a”)
| Method | What It Does | Example |
|---|---|---|
| .read() | Reads the entire file as a single string | content = file.read() |
| .readline() | Reads one line (including newline character) | line = file.readline() |
| .readlines() | Reads all lines into a list of strings | lines = file.readlines() |
Python
# Create a sample file first (for demonstration)
with open(“sample.txt”, “w”) as f:
f.write(“Line 1\nLine 2\nLine 3\n”)
# Read entire file
with open(“sample.txt”, “r”) as f:
content = f.read()
print(content)
# Output: Line 1
# Line 2
# Line 3
# Read line by line
with open(“sample.txt”, “r”) as f:
first_line = f.readline()
print(first_line) # Line 1 (includes newline)
# Read all lines into a list
with open(“sample.txt”, “r”) as f:
all_lines = f.readlines()
print(all_lines) # [‘Line 1\n’, ‘Line 2\n’, ‘Line 3\n’]
🕯️ Magic Note
The .read() method with no argument reads the entire file. For large files, this can use a lot of memory. A better approach for large files is to loop over the file object directly (each iteration gives one line) without reading the whole file at once.
Python
# Memory-efficient reading (line by line without readlines)
with open(“large_file.txt”, “r”) as f:
for line in f:
print(line.strip()) # .strip() removes newline
Python
# Write mode (overwrites)
with open(“output.txt”, “w”) as f:
f.write(“Hello, World!\n”)
f.write(“This is a new line.\n”)
# Append mode (adds to end)
with open(“output.txt”, “a”) as f:
f.write(“This line is appended.\n”)
# Writing multiple lines with writelines
lines = [“First line\n”, “Second line\n”, “Third line\n”]
with open(“output.txt”, “w”) as f:
f.writelines(lines)
Python
# Without ‘with’ (manual closing)
f = open(“file.txt”, “r”)
content = f.read()
f.close() # Easy to forget!
# With ‘with’ (automatic closing)
with open(“file.txt”, “r”) as f:
content = f.read()
# File is automatically closed here, even if an error occurred
Python
# Absolute path (full path from root)
with open(“/home/user/data.txt”, “r”) as f: # Linux/Mac
content = f.read()
# Windows path (use raw string or double backslashes)
with open(r”C:\Users\Name\data.txt”, “r”) as f: # raw string
content = f.read()
# Relative path (from current working directory)
with open(“data/subfolder/file.txt”, “r”) as f: # subfolder must exist
content = f.read()
🕯️ Magic Note
The best way to handle file paths across different operating systems is to use the pathlib module (Python 3.4+). It provides an object-oriented approach that works everywhere.
Python
from pathlib import Path
# Create a path object (works on any OS)
data_folder = Path(“data”)
file_path = data_folder / “config.txt” # Uses / operator
with open(file_path, “r”) as f:
content = f.read()
Python
import os
from pathlib import Path
# Using os.path
if os.path.exists(“data.txt”):
with open(“data.txt”, “r”) as f:
content = f.read()
else:
print(“File not found”)
# Using pathlib (more modern)
file_path = Path(“data.txt”)
if file_path.exists():
content = file_path.read_text() # Shortcut to read entire file
Python
# Reading a binary file (like an image)
with open(“image.jpg”, “rb”) as f:
binary_data = f.read()
print(len(binary_data)) # Number of bytes
# Writing a binary file
with open(“copy.jpg”, “wb”) as f:
f.write(binary_data)
Python
import os
from pathlib import Path
# Get current working directory
current = os.getcwd()
print(current)
# Change directory
os.chdir(“/path/to/new/directory”)
# List files in directory
files = os.listdir(“.”)
print(files)
# Create a new directory
os.mkdir(“new_folder”)
# Delete a file
os.remove(“unwanted.txt”)
# Rename a file
os.rename(“old_name.txt”, “new_name.txt”)
Python
try:
with open(“data.txt”, “r”) as f:
content = f.read()
except FileNotFoundError:
print(“The file does not exist”)
except PermissionError:
print(“You don’t have permission to read this file”)
except IOError as e:
print(f”An I/O error occurred: {e}”)
🕯️ Magic Note
The with statement also handles exceptions gracefully. If an error occurs inside the with block, the file is still closed before the exception propagates.
Python
# Simple note taking program
def save_note(filename, note):
with open(filename, “a”) as f:
f.write(note + “\n”)
print(“Note saved!”)
def read_notes(filename):
try:
with open(filename, “r”) as f:
notes = f.readlines()
for i, note in enumerate(notes, 1):
print(f”{i}. {note.strip()}”)
except FileNotFoundError:
print(“No notes found. Create one first!”)
# Usage
save_note(“my_notes.txt”, “Learn Python”)
save_note(“my_notes.txt”, “Master file I/O”)
read_notes(“my_notes.txt”)
- Forgetting to close files (use with to avoid this)
- Using “w” mode when you meant “a” (overwrites existing data)
- Forgetting newline characters \n in .write()
- Reading a file that does not exist (causes FileNotFoundError)
- Using backslashes in Windows paths without raw strings r”path”
- Assuming .read() removes newlines (it does not)
- Not handling file exceptions
- What is the difference between “w” and “a” modes?
- Why is the with statement recommended for file I/O?
- How do you read a file line by line without loading the entire file into memory?
- What exception is raised when you try to read a file that does not exist?
- Write code that appends “Hello” to a file named “log.txt”.
- What does .strip() do and why is it useful when reading files?
⚡ Whisper
A file is a memory that survives beyond the life of your program. When your code ends, the file remains. When you turn off your computer, the file remains. When you open the same file years later, your words are still there, waiting. This is permanence. This is the difference between RAM (remembering for a moment) and storage (remembering forever). Learn to write to files, and your programs gain memory. Learn to read from files, and your programs gain wisdom from past runs. A program that remembers is a program that grows. A program that forgets starts from zero every time. Choose to remember. Write to files. Read from files. Let your data live beyond a single execution. That is the conjure of persistence.