Exception Has Occurred: Filenotfounderror [Errno 2] No Such File or Directory: 'Data.Json'

opening json file error : FileNotFoundError: [Errno 2] No such file or directory

You have to specify the path like this :

 with open("./path_to_json_file/data.json", 'r') as f:
data = json.load(f)
f.close()



for key, value in data.items():
print("key: {} | value: {}".format(key, value))

FileNotFoundError: [Errno 2] JSON file

Use pathlib

  • This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
  • It's part of the standard library and should replace os.
  • Python 3's pathlib Module: Taming the File System
  • With cwd as WindowsPath('Users/MyUserName/Documents/davis/Homework_Repos/ETL-project/working')
    • cwd.parents[0] is WindowsPath('Users/MyUserName/Documents/davis/Homework_Repos/ETL-project')
    • cwd.parents[1] is WindowsPath('Users/MyUserName/Documents/davis/Homework_Repos')
from pathlib import Path
import json

cwd = Path.cwd()
file = cwd.parents[0] / 'Resources' / 'restaurant.json'

with file.open('r', encoding='utf-8') as f:
data = json.load(f.read())


Related Topics



Leave a reply



Submit