arrow
arrow
arrow
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?
Question

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?

A.

ab

B.

wb

C.

r+

D.

rb+

Correct option is C


The mode r+ is used to open a binary file for both reading and writing in Python. It allows you to read and write to the file without truncating it. If the file does not exist, it will raise an error.
Important Key Points:
1. r+: Opens the file for both reading and writing. The file must already exist; if it doesn't, an error is raised. It doesn't truncate the file (i.e., data is not deleted when the file is opened).
2. Binary Mode (b): When working with binary files, you need to add the b character to the mode (like rb, wb, etc.) to indicate binary mode.
3. Writing to Files: To write to a file in binary mode, use wb (write binary) or rb+ (read and write binary).
Knowledge Booster:
· Option (a): ab is used to open a binary file in append mode for writing. It does not allow reading from the file.
· Option (b): wb opens a binary file for writing, but it does not allow reading from it. It truncates the file if it exists.
· Option (d): rb+ is similar to r+ but opens the file in binary mode. This is correct for binary files, but r+ works for both text and binary files, so r+ is typically more flexible.

Free Tests

Free
Must Attempt

CDP Subject Test 01

languageIcon English
  • pdpQsnIcon20 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt

EVS Subject Test 01

languageIcon English
  • pdpQsnIcon20 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon20 Mins
languageIcon English
Free
Must Attempt

CTET Paper-I PYP (07 July 2024)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘EMRS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
289k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow