Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Python - File Handling Fundamentals
Identify the error in this code snippet:

f = open("file.txt", "r")
f.write("Hello")
f.close()
AFile is not closed properly
BFile is opened in read mode but write is attempted
CSyntax error in open function
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check file mode

    File opened with "r" mode allows reading only, no writing.
  2. Step 2: Identify operation error

    Calling write() on a read-only file causes an error.
  3. Final Answer:

    File is opened in read mode but write is attempted -> Option B
  4. Quick Check:

    Write requires write mode, not read [OK]
Quick Trick: Write needs "w" or "a" mode, not "r" [OK]
Common Mistakes:
  • Trying to write in read mode
  • Ignoring file close
  • Assuming open syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes