0

Folder structure:

MainFolder
    __init__.py
    FolderA
        Config.json
        ConfigHelper.py
        testA.py
    FolderB
        Test.py

ConfigHelper.py file:

import json
class ConfigHelper:
    def read_config(self):
        fileName = "Config.json"
        with open(fileName, "r") as jsonfile:
            return json.load(jsonfile)

When calling ConfigHelper.read_config() function from FolderB-->Test.py file , then Getting Error: No such file or directory: 'config.json' But if calling it from TestA.py file, No error is coming. Look like it is taking path relative from the calling place. Please tell me , How I can fix it?

3 Answers3

1

When you are in FolderB there is no config.json file there.

Solutions

  • Run your script from MainFolder and pass correct path (FolderA/config.json).
  • Add folders to path, see.
  • Change working directory to FolderB before trying to read config.json, see.
bekirbakar
  • 166
  • 2
  • 7
0

You need to provide the full path to the file. You are right that it is looking for the relative path. When you are just passing the Config.json path it is looking in your current working directory for this.

0
 path = os.getcwd()
 print("Current Directory", path)
 filepath = path + "/MainFolder/FolderA/Config.json"

Gave Full Path as above to fix the problem.