0%

15- File I/O in Python

Read from files. Write to files. Save data between program runs. File I/O is how your program talks to the permanent storage of your computer.

Your program runs. It creates variables, processes data, makes decisions. Then it ends. Everything disappears. The variables vanish. The计算结果 are gone. This is the nature of RAM (short-term memory). But sometimes you want to save data. A user’s settings. A high score. A log of what happened. A configuration file. A list of names. This is where files come in. File I/O (Input/Output) is how Python reads from and writes to files on your hard drive. Data written to a file remains there even after your program ends. The next time you run the program, you can read that data back. Python makes file handling simple. You open a file. You read from it or write to it. Then you close it. Three steps. That is all.

🕯️ 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).

Opening Files: The open() Function
The open() function is the gateway to file I/O. It takes a file path and a mode, and returns a file object.

Python

# Basic syntax

file = open(“filename.txt”, “r”)

# Do something with file

file.close()

⚠️ Always close files when you are done. Leaving files open can cause memory leaks and prevent other programs from accessing the file. The .close() method is essential.
File Modes
The mode tells Python what you want to do with the file. Choose the right mode for your task.
ModeWhat It DoesFile Must ExistCreates File
“r”Read only (default)YesNo
“w”Write (overwrites existing content)NoYes
“a”Append (adds to end of file)NoYes
“x”Exclusive creation (fails if file exists)NoYes
“r+”Read and writeYesNo
“w+”Write and read (overwrites)NoYes
“a+”Append and readNoYes

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”)

💡 Always specify the mode explicitly. While “r” is the default, being explicit makes your code more readable. For text files (the default), use the modes above. For binary files (images, videos), add a b like “rb” or “wb”.
Reading Files
Python provides several methods to read content from files. Choose based on your needs.
MethodWhat It DoesExample
.read()Reads the entire file as a single stringcontent = file.read()
.readline()Reads one line (including newline character)line = file.readline()
.readlines()Reads all lines into a list of stringslines = 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

Writing to Files
Use .write() to write strings to a file. Use .writelines() to write a list of strings.

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)

⚠️ .write() does NOT automatically add newlines. You must include \n explicitly. Forgetting this will write everything on one line.
The with Statement (Best Practice)
The with statement is the recommended way to work with files. It automatically closes the file for you, even if an error occurs. No more forgetting .close().

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

💡 Always use with when working with files. It is cleaner, safer, and more Pythonic. You will almost never need to call .close() manually when you use with.
Working with Different File Paths
File paths can be tricky, especially across different operating systems. Python provides tools to handle paths correctly.

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()

Checking if a File Exists
Before reading a file, you may want to check if it exists to avoid errors. Use the os.path module or pathlib.

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

💡 Pathlib offers convenient shortcuts: .read_text() reads the entire file as a string, and .write_text() writes a string to a file.
Binary Files (Images, Videos, etc.)
For binary files, add b to the mode. This prevents Python from trying to decode the content as text.

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)

Common File Operations
Here are common operations you might need when working with files and directories.

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”)

Error Handling with Files
File operations can fail. The file might not exist. You might not have permission. Using try-except blocks makes your program robust.

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.

Practical Example: A Simple Note Keeper
Here is a complete example combining many file I/O concepts.

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”)

Common Mistakes with File I/O
  • 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
Check Your Understanding
  • 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.

Related posts