Bird
Raised Fist0

Identify the error in this code snippet that tries to read JSON data from a file:

medium📝 Debug Q14 of Q15
Python - Structured Data Files

Identify the error in this code snippet that tries to read JSON data from a file:

import json

f = open('data.json', 'r')
data = json.load(f)
f.close()
print(data)

AMissing import statement for json
BFile not opened in write mode
CFile not closed properly
DNo error, code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check import and file open

    The code imports json and opens the file in 'r' mode correctly.
  2. Step 2: Verify json.load and file close

    json.load reads JSON data properly, and file is closed with f.close().
  3. Final Answer:

    No error, code works correctly -> Option D
  4. Quick Check:

    Code reads JSON file correctly [OK]
Quick Trick: json.load needs file opened in 'r' mode [OK]
Common Mistakes:
MISTAKES
  • Forgetting to import json
  • Opening file in wrong mode
  • Not closing the file after reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes