Bird
0
0

What is the mistake in the following Python code snippet that attempts to read the entire content of 'info.txt'?

medium📝 Debug Q6 of 15
Python - File Reading and Writing Strategies
What is the mistake in the following Python code snippet that attempts to read the entire content of 'info.txt'?
file = open('info.txt', 'r')
content = file.read
print(content)
file.close()
AThe file is opened in write mode instead of read mode.
BThe <code>read</code> method is not called; parentheses are missing.
CThe file is not closed after reading.
DThe variable <code>content</code> is overwritten before printing.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code

    The code assigns file.read (a method object) to content without calling it.
  2. Step 2: Identify the error

    Missing parentheses means content is a method reference, not the file content string.
  3. Step 3: Correct usage

    It should be content = file.read() to actually read the file content.
  4. Final Answer:

    The read method is not called; parentheses are missing. is correct.
  5. Quick Check:

    Always call read() with parentheses to get content [OK]
Quick Trick: Call read() with parentheses to read file content [OK]
Common Mistakes:
  • Assigning the method itself instead of calling it.
  • Forgetting to close the file (though here it is closed).
  • Opening file in wrong mode (not the case here).

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes