Bird
Raised Fist0

Find the error in this code:

medium📝 Debug Q7 of Q15
Python - File Handling Fundamentals
Find the error in this code:
f = open('notes.txt', 'w')
f.write('Hello')
f.read()
f.close()
Awrite() returns None
BFile not closed properly
CCannot read from a file opened in write mode
DMissing file path
Step-by-Step Solution
Solution:
  1. Step 1: Check file mode

    File is opened in 'w' mode which is write-only.
  2. Step 2: Calling read() on write-only file

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

    Cannot read from a file opened in write mode -> Option C
  4. Quick Check:

    Write mode disallows reading = A [OK]
Quick Trick: Don't read from files opened in 'w' mode [OK]
Common Mistakes:
MISTAKES
  • Trying to read after write without reopening
  • Ignoring file mode restrictions
  • Assuming write() returns data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes