Bird
Raised Fist0

You want to read a file and then write new content to it without losing the original data. Which mode should you use to open the file?

hard🚀 Application Q8 of Q15
Python - File Handling Fundamentals
You want to read a file and then write new content to it without losing the original data. Which mode should you use to open the file?
A'w+'
B'r+'
C'a'
D'x'
Step-by-Step Solution
Solution:
  1. Step 1: Understand modes that allow reading and writing

    'r+' opens file for reading and writing without truncating the file.
  2. Step 2: Compare with other modes

    'w+' truncates file, 'a' appends only, 'x' creates new file and errors if exists.
  3. Final Answer:

    'r+' -> Option B
  4. Quick Check:

    Read/write without truncating = 'r+' = B [OK]
Quick Trick: Use 'r+' to read and write without erasing file [OK]
Common Mistakes:
MISTAKES
  • Using 'w+' which erases file
  • Using 'a' which only appends
  • Using 'x' which fails if file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes