Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
Python - File Handling Fundamentals
Identify the error in the following code snippet:
file = open('notes.txt', 'r')
content = file.read()
print(content)
file.write('More notes')
file.close()
ATrying to write to a file opened in read mode causes an error.
BThe file is not closed before reading.
CThe print statement is incorrect syntax.
DThe file name should be in double quotes.
Step-by-Step Solution
Solution:
  1. Step 1: Check file mode and operations

    The file is opened in 'r' (read) mode, which does not allow writing.
  2. Step 2: Identify the invalid operation

    Calling file.write() on a file opened for reading causes a runtime error.
  3. Final Answer:

    Trying to write to a file opened in read mode causes an error. -> Option A
  4. Quick Check:

    Write not allowed in 'r' mode [OK]
Quick Trick: Write only in 'w' or 'a' modes, not 'r' [OK]
Common Mistakes:
  • Trying to write in read mode
  • Ignoring file close after reading
  • Thinking quotes style matters for filename

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes