I’m currently working on a Python project where I need to load a JSON configuration file. I’m using the following function to load the JSON file:
def load_config(self, file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
self.config_data.update(data)
except FileNotFoundError:
print(f"File not found: {file_path}")
except json.JSONDecodeError:
print(f"Invalid JSON format: {file_path}")
However, when I attempt to load my JSON file located at ‘defmet_common/configs/config.edi_db_servers.json’, I keep encountering a FileNotFoundError.
Interestingly, when I copy the relative path of the file from my file explorer, I get ‘defmet_commonconfigsconfig.edi.db_servers.json’ which uses backslashes and seems to have a different file extension.
I have verified that the file does exist at the specified location and has the .json extension. I’m running the script from the same directory that contains the defmet_common folder.
Why am I encountering this issue and how can I resolve it?