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
asWindowsPath('Users/MyUserName/Documents/davis/Homework_Repos/ETL-project/working')
cwd.parents[0]
isWindowsPath('Users/MyUserName/Documents/davis/Homework_Repos/ETL-project')
cwd.parents[1]
isWindowsPath('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
Windowserror: [Error 126] the Specified Module Could Not Be Found
How to Check Url Change With Selenium in Python
How to Make a Grade Calculator in Python
How to Locate the Input Within Div
Append Dataframes Together in for Loop
Sum of Square Differences (Ssd) in Numpy/Scipy
How Can One Modify the Outline Color of a Node in Networkx
How to Use Authenticated Proxy in Selenium Chromedriver
How to Compile Multiple Python Files into Single .Exe File Using Pyinstaller
Use Tqdm Progress Bar With Pandas
Psycopg2 Insert Python Dictionary as Json
How to Ignore Null Byte When Reading a CSV File
How to Get Rid of the B-Prefix in a String in Python
Get the Last Sunday and Saturday'S Date in Python
Element That Appear More That Once in the List in Python