Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Python - File Reading and Writing Strategies
What is wrong with this code?
with open('info.txt') as f:
    line = f.readline()
print(line)
AMissing loop to read all lines
Bprint(line) is outside the with block
Creadline() reads all lines, not one
DFile not opened in write mode
Step-by-Step Solution
Solution:
  1. Step 1: Understand readline() behavior

    readline() reads only one line, not the whole file.
  2. Step 2: Identify missing loop for multiple lines

    To read all lines, a loop is needed to call readline() repeatedly.
  3. Final Answer:

    Missing loop to read all lines -> Option A
  4. Quick Check:

    readline() reads one line; loop needed for all = B [OK]
Quick Trick: Use loop with readline() to read all lines [OK]
Common Mistakes:
  • Assuming readline() reads whole file
  • Ignoring need for loop
  • Confusing with readlines()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes