0
import json

with open("data.json", "r") as f:
    data = json.load(f)

print(data)

Thats the py code.

{
    "test":"Hi"
    "things": [
        {
            "name":"Stack"
            "lastname":"Overflow"
        }
    ]
}

That's the JSON file.

The JSON and python file are in the same folder and I don't get it why it does not find the file.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

The problem might be that you are not running the script from the directory where these files are present.

Attempt 1:

You can try to run the script from the same directory as those files and it should work.

Attempt 2:

You can use the absolute path of the json file instead of just specifying the file name. You can modify your code like this :

import json
from os import path

file_path = path.abspath(__file__) # full path of your script
dir_path = path.dirname(file_path) # full path of the directory of your script
json_file_path = path.join(dir_path,"data.json") # absolute file path of json file


with open(json_file_path, "r") as f:
    data = json.load(f)

print(data)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22