Which code should you use?

You develop a Python application for your school.
You need to read and write data to a text file. If the file does not exist, it must be created. If the file has content, the content must be removed.
Which code should you use?
A. open(“local_data”, “r”)
B. open(“local_data”, “r+”)
C. open(“local_data”, “w+”)
D. open(“local_data”, “w”)

microsoft-exams

One thought on “Which code should you use?

  1. Ans : C. open(“local_data”, “w+”)

    w- Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
    w+- Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.