I have the below structure
sample.py pack/ -app.py -test.json
from pack import app
app.print_name()
import json
def print_name():
with open(file=r"test.json",mode='r') as json_file :
py_dict = json.load(json_file)
print(py_dict["name"])
print_name()
{
"name" : "Rajkumar"
}
Now, if I just run => python app.py. I'm getting FileNotFoundError
You need to add absolute path to the test.json.
app.py:
import json
import os
def print_name():
dir_path = os.path.dirname(__file__)
with open(f"{dir_path}/test.json", mode='r') as json_file:
py_dict = json.load(json_file)
print(py_dict["name"])