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.