Bird
0
0

Find the bug in this code snippet:

medium📝 Debug Q7 of 15
Python - File Handling Fundamentals
Find the bug in this code snippet:
with open('output.txt', 'w') as f:
    f.write(12345)
AMissing file close statement.
BFile mode 'w' cannot be used with write().
Cwrite() requires a string, not an integer.
Dopen() needs a second argument for encoding.
Step-by-Step Solution
Solution:
  1. Step 1: Check argument type for write()

    write() expects a string, but 12345 is an integer.
  2. Step 2: Other points

    'w' mode is correct, with block auto-closes file, encoding optional.
  3. Final Answer:

    write() requires a string, not an integer. -> Option C
  4. Quick Check:

    write() argument must be string [OK]
Quick Trick: Convert non-strings to str() before writing [OK]
Common Mistakes:
  • Passing int directly
  • Forgetting with auto-close
  • Assuming encoding always needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes