Bird
Raised Fist0

What is wrong with this code?

medium📝 Debug Q7 of Q15
Python - File Handling Fundamentals
What is wrong with this code?
with open('data.txt', 'r') as file:
    data = file.read
print(data)
Aread should be called as a method with parentheses
BFile should be opened in write mode
CIndentation error in print statement
DVariable 'data' is undefined
Step-by-Step Solution
Solution:
  1. Step 1: Check how read() is used

    read is a method and must be called with parentheses to get content.
  2. Step 2: Identify consequence of missing parentheses

    Without (), data stores method object, not file content, causing wrong output.
  3. Final Answer:

    read should be called as a method with parentheses -> Option A
  4. Quick Check:

    Method calls need () to execute [OK]
Quick Trick: Add () to call read() method [OK]
Common Mistakes:
MISTAKES
  • Forgetting () after read
  • Opening file in wrong mode
  • Misplaced print indentation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes