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”)
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.