Skip to content
Activities · Activity 2.2

Q.Some of the other file access modes are <rb+>, <wb>, <w+>, <ab>, <ab+>. Find out for what purpose each of these are used. Also, find the file offset positions in each case.

Yanam BieapTextbookSubjective· 3mImportance★★★★★
9% · 2/22 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

A reference guide to Python's file access modes, explaining when to use each mode and where the file pointer (offset) starts.

Why File Access Modes Matter

When you open a file in Python, you must tell the interpreter what you intend to do with it. The mode string controls three critical behaviors: whether you can read, whether you can write, and what happens to existing content. Getting the mode wrong leads to runtime errors (trying to write to a read-only handle) or data loss (accidentally truncating a file you meant to append to).

The mode also determines the file offset — the position from which the next read or write operation begins. Understanding this offset is essential when working with binary files or when you need to both read and modify data in a single session.

The File Access Modes

ModeNameRead?Write?Creates if missing?Truncates existing?Initial offsetBinary/Text
rb+Read-binary-plus✓✓✗ (raises error)✗Start (0)Binary
wbWrite-binary✗✓✓✓Start (0)Binary
w+Write-plus✓✓✓✓Start (0)Text
abAppend-binary✗✓✓✗End (EOF)Binary
ab+Append-binary-plus✓✓✓✗End (EOF)Binary

rb+ — Read-Binary-Plus (Update Mode)

Opens an existing binary file for both reading and writing. The file must already exist; if it doesn't, Python raises FileNotFoundError. Nothing is erased. The pointer starts at byte 0, so your first read gets the beginning of the file.

Use case: You have a binary database file (say, a pickled dictionary or a custom binary format) and need to read some records, then overwrite specific bytes without destroying the rest.

# Assume 'data.bin' exists with content b'ABCDEFGH'
with open('data.bin', 'rb+') as f:
    print(f.tell())          # 0 — starts at beginning
    first_two = f.read(2)    # reads b'AB'
    print(first_two)
    f.write(b'XY')           # overwrites 'CD' with 'XY'
    # File now contains b'ABXYEFGH'

Output:

0
b'AB'
Watch out

rb+ will not create the file. If you try to open a non-existent file, the program crashes. Always check existence first or use a mode that creates.


wb — Write-Binary

Opens a file for writing in binary mode. If the file exists, its content is immediately truncated to zero bytes the moment you open it. If it doesn't exist, Python creates it. You can only write; any attempt to read raises io.UnsupportedOperation.

Use case: Saving a fresh binary blob — an image, a serialized object, a compiled bytecode file — where you don't care about the old content.

with open('output.bin', 'wb') as f:
    print(f.tell())       # 0
    f.write(b'\x89PNG')   # writes PNG magic number
    # File now contains exactly those 4 bytes

Output:

0

The offset starts at 0 because the file is empty (just truncated or newly created).


w+ — Write-Plus (Text Mode)

Opens a file for both reading and writing in text mode (newline translation happens on Windows). Like wb, it truncates existing content to zero. The pointer starts at position 0 of the now-empty file.

Use case: You want to generate text, then immediately read it back (perhaps for validation) without closing and reopening the file.

with open('temp.txt', 'w+') as f:
    f.write("Hello\nWorld\n")
    f.seek(0)                # rewind to start
    content = f.read()
    print(repr(content))

Output:

'Hello\nWorld\n'
Tip

After writing, the pointer is at the end. You must seek(0) before reading, or you'll get an empty string.


ab — Append-Binary

Opens a file for writing in binary mode, but the pointer is positioned at the end of the file (EOF). Every write appends new bytes; you cannot overwrite existing data. If the file doesn't exist, it is created. Reading is not allowed.

Use case: Logging binary events to a file, or concatenating binary chunks (e.g., streaming video segments).

# Assume 'log.bin' exists with b'START'
with open('log.bin', 'ab') as f:
    print(f.tell())       # 5 (length of 'START')
    f.write(b'END')
    # File now contains b'STARTEND'
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.