Which of the following command is used to open file "c:\comp.txt" for writing in binary format only?
Which of the following command is used to open file "c:\comp.txt" for writing in binary format only?
Solution
✅ Correct Option: 4
The mode string in open() combines an action with a file type. "w" means write (it creates the file or erases an existing one), and "b" means binary. So "wb" opens the file for writing in binary format only.
Option 1: "w" writes in text mode, since no "b" is given.
Option 2: "w+" allows both writing and reading, and it is also in text mode.
Option 3: "r" is read-only text mode, so it cannot write.
If "b" is absent, Python assumes text mode ("t") by default.
Related questions:
2025: 27 May Shift 2
2026: 26 May Shift 1
2026: 19 May Shift 1