Bird
Raised Fist0

Identify the error in this code snippet:

medium📝 Debug Q6 of Q15
Python - File Reading and Writing Strategies
Identify the error in this code snippet:
file = open('log.txt')\nfor line in file:\nprint(line)
AIndentation error in print statement
BMissing file close() method
CWrong file mode for reading
DFile name should be in double quotes
Step-by-Step Solution
Solution:
  1. Step 1: Check indentation of loop body

    Python requires the print inside the for loop to be indented.
  2. Step 2: Identify missing indentation

    print(line) is not indented, causing syntax error.
  3. Final Answer:

    Indentation error in print statement -> Option A
  4. Quick Check:

    Python needs indented loop body = B [OK]
Quick Trick: Indent inside loops to avoid syntax errors [OK]
Common Mistakes:
MISTAKES
  • Ignoring indentation rules
  • Thinking file must be closed manually
  • Confusing quotes usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes