Question:

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: Sep 18, 2025
  • file.open("data.txt")
  • file.open("data.txt","r+")
  • file.open("data.txt","rw")
  • file.open("data.txt","rw+")
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Understanding Python file modes.
- \texttt{"r"} → read only.
- \texttt{"w"} → write only (overwrites file).
- \texttt{"r+"} → read and write (file must exist).
- \texttt{"w+"} → write and read (creates new file if not exists, truncates existing).

Step 2: Check options.
- Option 1: Missing mode, defaults to "r" (read-only). Wrong.
- Option 2: \texttt{"r+"} is correct for read and write mode.
- Option 3: \texttt{"rw"} is not a valid mode in Python. Wrong.
- Option 4: \texttt{"rw+"} is also invalid in Python. Wrong.

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

Questions Asked in CUET exam

View More Questions