I am trying to import a config json file from a different directory from where i want to use it. Am getting this error :
with open('../../config/config.json', 'r') as f: IOError: [Errno 2] No such file or directory: '../../config/config.json'
This is what I did. I tried importing JSON library and load the file as shown in the code below.
import json
with open('../../config/config.json', 'r') as f:
config = json.load(f)
Any help will be appreciated
You need to insert complete path to import the file successfully.
The issue is with your ../../config
Instead give the complete path of the file.
What you're doing does work, however isn't great practice and is dependent on other variables for it to work. I'd suggest putting the full (absolute) path of the file you want to read in:
with open('the/full/path/to/config/config.json', 'r') as f:
config = json.load(f)
Alternatively you can build the path using the sys
module and assign it to a variable to be used in the open
call, I'd suggest looking at the docs
did you try whether that is the correct path? pathlib.Path
can help you here
from pathlib import Path
parent = Path("Path("../../config/"
parent.exists(), parent.is_dir()
p = parent / "config.json"
p.exists()
You can use pandas to read Json format data.
import pandas as pd
pd.read_json('<PATH>')
Note Use path like: ././config/config.json and call os.chdir('..') before accessing file.
For reference click here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.