Home > Blockchain >  Error in loading JSON data - can't find the file
Error in loading JSON data - can't find the file

Time:10-23

I've been trying to solve this issue, but right now I need some help. I'm trying to upload this JSON file (DBL) in Spyder IDE. I have stored the JSON-data file and the Spyder file, in the same map in order to read the JSON file, but it's not working.

My Python code:

import json 

file = open("dbl")

dbl = json.load(file)

print(dbl)

Every time I upload the json file, in the same map as the spyder.py file, it can't recognize the file directory.

I have stored the my .py file in the same folder as the JSON file.

This is the error message:

FileNotFoundError: [Errno 2] No such file or directory: 'dbl.json'

CodePudding user response:

You'll use ".json" in the file path. For example:

file = open("dbl.json")

CodePudding user response:

if dbl is name of json file then you should add ".json" extension too. you might do this:

   
# Opening JSON file
f = open('dbl.json',)
   
# returns JSON object as 
# a dictionary
data = json.load(f)
   
# Iterating through the json
# list
for i in data:
    print(i)
   
# Closing file
f.close()```



CodePudding user response:

The code is fine, however, it's good practice to add a file extension. Looks like you forgot to add the extension. You are using relative paths. It is advised to use absolute paths. In this case, put the python script and db1 file in the same directory and try again.

In case, you want to debug just add the below code on top of your script to see if the file is present or not and modify the script accordingly.

import os; 
print(os.listdir())

CodePudding user response:

The file, in fact, does not exist. The actual filename is dbl.json.json.

import json 
file = open("dbl.json.json")
dbl = json.load(file)
print(dbl)
  • Related