Question:medium

Identify the correct python statement to open a text file \texttt{"data.txt"} in both read and write mode.

Show Hint

Use \texttt{"r+"} for read+write, \texttt{"w+"} for write+read (new file), and \texttt{"a+"} for append+read in Python.
Updated On: Feb 15, 2026
  • file.open("data.txt")
  • file.open("data.txt","r+")
  • file.open("data.txt","rw")
  • file.open("data.txt","rw+")
Show Solution

The Correct Option is B

Solution and Explanation

Step 1: Python File Modes Explained.
- \texttt{"r"} → read-only access.
- \texttt{"w"} → write-only access (overwrites existing content).
- \texttt{"r+"} → read and write access (requires the file to exist).
- \texttt{"w+"} → write and read access (creates the file if it doesn't exist, truncates if it does).

Step 2: Evaluate Options.
- Option 1: Omitting the mode defaults to "r" (read-only). Incorrect.
- Option 2: \texttt{"r+"} correctly specifies read and write mode.
- Option 3: \texttt{"rw"} is not a recognized file mode in Python. Incorrect.
- Option 4: \texttt{"rw+"} is also an invalid Python file mode. Incorrect.

Conclusion: \[\boxed{\texttt{file.open("data.txt","r+")}}\]
Was this answer helpful?
0