-1

I'm new to coding so I followed a tutorial for a chatbot, I need to import my intentions (which I named intentii, in my language) intentii.json into the chatbot, so I'm using:

intentii = json.loads(open('intentii.json').read())

I have seen other questions about the error I said in the title, and yes, I made sure the name is typed right, it's in the same exact directory as all my files including this chatbot one are, and still, it says it can't find the file, I tried placing the whole directory path and it seemed to work ( I didn't get the same error again, but an unicode error which I believe is another problem related to my .json file ), but I cannot use the whole path because I have to send this to my teacher and, of course, he won't have the same exact path as mine.

What is the solution?

Edit: Also, I've noticed that it says that for every file I need to access from the folder

Kileps
  • 1
  • 2

2 Answers2

0

Try this function to load a json file:

import json

def load_json(filepath):
    with open(filepath) as f:
        data = json.load(f)
    return data

Then

intentii = load_json("intentii.json")
User One
  • 287
  • 2
  • 9
  • Sorry for the late reply! If I understand correctly, I have to input the .json file's file path in (filepath)? If so, does that mean that when my teacher runs it, it won't work as the file path is different? If I don't have to type the file path there, well, I just tried that and it still says it can't find it – Kileps Jul 13 '22 at 17:55
  • Can you put a screenshot of your project's file structure? – User One Jul 13 '22 at 19:56
  • Here's a link with a few screenshots: https://imgur.com/a/7E231kK edit: I mentioned 2nd screenshot in one of the descriptions, but imgur messed up the screenshot order – Kileps Jul 13 '22 at 20:20
0

Try using this python os module to find dir_root where current file is located and then combine it json file name if it is stored in same directory.

import json
import os

dir_root = os.path.dirname(os.path.abspath(__file__))
intentii1 = json.loads(open(dir_root+'\js.json').read())
  • I'll give this a try, although I am very confused as to why it doesn't work like the code I mentioned, because I have 2 files ( a training one and a chatbot one ) and in the training file, which is located in the exact same folder, the line worked and python was able to find my intentii.json file, meanwhile it doesn't work for the chatbot file – Kileps Jul 13 '22 at 20:22
  • nvm had to add (.\) as in .\filename and it worked :D – Kileps Jul 13 '22 at 21:38
  • you can do that also because (.) means the directory at which your current file is located so it is same. – Siddharth Asodariya Jul 15 '22 at 06:04