Which out of the following mode is required to be used while opening a binary…

2023

Which out of the following mode is required to be used while opening a binary file to allow read as well as write operations in a Python program?

  1. A.

    ab

  2. B.

    wb

  3. C.

    r+

  4. D.

    rb+

Attempted by 1298 students.

Show answer & explanation

Correct answer: D

Answer: rb+ opens a binary file for both reading and writing without truncating the file.

Key details:

  • rb+ opens the file in binary mode and allows both reading and writing. It does not truncate the existing file content.

  • The file pointer is placed at the beginning, so you can read existing data or overwrite from the start.

  • Equivalent mode: 'r+b' is the same as 'rb+'.

  • Other related modes (for contrast):

    • wb — open in binary write mode and truncate the file (write-only).

    • ab — open in binary append mode (write-only), does not allow reading.

    • r+ — open for reading and writing but in text mode (not binary); use rb+ for binary read/write.

Explore the full course: Rssb Senior Computer Instructor