How to read json file in custom action python file in rasa

Hi… I want to read the json file in custom action file to do the configuration.

class ActionReadFile(Action): def name(self): return “action_Read_File”

def run(self, dispatcher, tracker, domain):
    try:
        with open("config_mysql.json", "r") as jsonfile:
             config_data = json.load(jsonfile) # Reading the file
             print("Read successful")
             jsonfile.close()
             data=config_data["dev"]   

The folder structure:

opt/RasaBot/actions/action_Read_File.py opt/RasaBot/config_mysql.json

But I am getting [Errno 2] No such file or directory: ‘opt/ChatBot/config_mysql.json’

In command prompt : The current directory is opt/RasaBot

Could you help anyone How to overcome this issue

What confuses me ist that you open “config_mysql.json”, but the error states ‘opt/ChatBot/config_mysql.json’. If your current folder is “opt/RasaBot” then it should state that “config_mysql.json” is not available without the mention of some path before the actual file name. I would check your setup again. Often it helps to use an absolute path the the file in your open statement.

Additionally, you do not have to use jsonfile.close(), because the with open-statenebt already closes the file at the end. Although, this should not the cause of the error, it makes your code more readable.

1 Like