Bird
0
0

What is wrong with this code snippet for reading JSON data?

medium📝 Debug Q7 of 15
Selenium Python - Data-Driven Testing
What is wrong with this code snippet for reading JSON data?
import json
with open('test.json', 'w') as f:
    data = json.load(f)
AFile name is incorrect
Bjson.load() cannot be used inside with statement
CMissing json.loads() call
DFile opened in write mode instead of read mode
Step-by-Step Solution
Solution:
  1. Step 1: Check file open mode

    The file is opened with mode 'w' (write), which empties the file and is not suitable for reading.
  2. Step 2: Correct mode for reading is 'r'

    To read JSON data, open the file with mode 'r' or omit mode (default is 'r').
  3. Final Answer:

    File opened in write mode instead of read mode -> Option D
  4. Quick Check:

    Open file in 'r' mode to read JSON [OK]
Quick Trick: Open JSON files in read mode ('r') to load data [OK]
Common Mistakes:
  • Opening file in write mode
  • Using json.loads() on file object
  • Not closing file

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes