Bird
0
0

Identify the error in this code:

medium📝 Debug Q6 of 15
Python - Structured Data Files

Identify the error in this code:

import json

with open('data.json', 'w') as f:
    data = json.load(f)

AMissing import statement
BFile opened in write mode but json.load() expects readable file
Cjson.load() should be json.loads()
DNo error
Step-by-Step Solution
Solution:
  1. Step 1: Check file mode for reading JSON

    The file is opened in write mode ('w'), which does not allow reading.
  2. Step 2: json.load() requires a readable file object

    Using json.load(f) on a write-only file causes an error.
  3. Final Answer:

    File opened in write mode but json.load() expects readable file -> Option B
  4. Quick Check:

    json.load() needs file opened in read mode [OK]
Quick Trick: Open file in read mode before using json.load() [OK]
Common Mistakes:
  • Opening file in wrong mode
  • Confusing load and loads

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes