Skip to main contentSkip to solution

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question