Python
File Handling
Programming
Open Function
Coding Skills

Difference between modes a, a+, w, w+, and r+ in built-in open function?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python file modes look short, but each one encodes several behaviors at once: whether the file must already exist, whether writes append or overwrite, and whether reading is allowed. The modes a, a+, w, w+, and r+ are easy to confuse because they all involve writing, yet they differ in ways that can either preserve data or destroy it immediately.

w and w+ Truncate the File

The most important fact about w and w+ is that they clear existing file content as soon as the file is opened. w allows writing only. w+ allows both reading and writing after the truncation.

python
1with open("demo.txt", "w", encoding="utf-8") as handle:
2    handle.write("fresh content\n")
3
4with open("demo.txt", "w+", encoding="utf-8") as handle:
5    handle.write("replaced again\n")
6    handle.seek(0)
7    print(handle.read())

These modes are correct when you truly want replacement. They are dangerous when you only intended to update part of a file.

a and a+ Preserve Existing Data

Append modes keep the current contents and always add new writes to the end of the file. a is write-only append. a+ adds read support.

python
1with open("demo.txt", "a", encoding="utf-8") as handle:
2    handle.write("new log line\n")
3
4with open("demo.txt", "a+", encoding="utf-8") as handle:
5    handle.seek(0)
6    print(handle.read())
7    handle.write("another line\n")

The subtle point is that reads can move the file pointer, but writes in append mode still go to the end. That surprises people who expect seek to change where later writes land.

r+ Updates an Existing File In Place

r+ opens a file for both reading and writing without truncating it. Unlike w or a, it expects the file to already exist. That makes it useful for controlled updates when replacement is not desired.

python
1with open("demo.txt", "r+", encoding="utf-8") as handle:
2    existing = handle.read()
3    handle.seek(0)
4    handle.write("HEADER\n" + existing)

Because r+ does not clear the file automatically, you must manage pointer position and leftover content carefully. If the new content is shorter than the old content and you do not rewrite or truncate the rest, stale bytes may remain.

Choose by Intent, Not by Habit

The simplest decision rule is to describe the operation before you pick the mode:

  • Replace everything: use w or w+.
  • Add new content without deleting the old: use a or a+.
  • Read and modify an existing file in place: use r+.

This sounds obvious, but many bugs come from defaulting to w because it is familiar. That habit is especially risky in scripts that touch configuration, logs, or user data.

Mixed Read and Write Flows Need Explicit seek

When a file mode allows both reading and writing, pointer movement matters. The code becomes much easier to reason about when seek is explicit at every boundary where the script switches from reading to writing or back again.

python
1from pathlib import Path
2
3Path("mode_demo.txt").write_text("start\n", encoding="utf-8")
4
5with open("mode_demo.txt", "r+", encoding="utf-8") as handle:
6    data = handle.read()
7    handle.seek(0)
8    handle.write("prefix\n" + data)
9
10print(Path("mode_demo.txt").read_text(encoding="utf-8"))

Small verification scripts like this are worth running when you are not completely sure how a mode behaves. They expose truncation and append semantics faster than guessing from memory.

Common Pitfalls

The biggest mistake is opening a file with w when the code was supposed to preserve existing contents. Another is expecting a+ writes to follow the current pointer position after a read. People also misuse r+ on files that may not exist, or forget that in-place updates can leave unwanted trailing content behind.

Summary

  • 'w and w+ truncate the file immediately.'
  • 'a and a+ preserve existing content and append new writes.'
  • 'r+ reads and writes an existing file without automatic truncation.'
  • Mixed read-write code should move the file pointer explicitly with seek.
  • Pick the mode that matches the operation you actually want, not the one you use most often.

Course illustration
Course illustration

All Rights Reserved.