Skip to main contentSkip to solution

Which function is used to write in a file without over writing the previous content?

Solution

✅ Correct Option: 1

Append adds new content at the end of a file and keeps what is already there, whereas write mode ("w") erases the old content first. In Python this is done by opening the file in append mode, open("file.txt", "a")\texttt{open("file.txt", "a")}, and then writing to it.


Option 3: add() is a set method in Python, not a file operation.

Options 2 and 4: read() only reads data, and close() only closes the file. Neither writes anything.

Keyboard Shortcuts

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