-1

I get this when I try to run my python project code

with open("config.json", "r") as f:

Exception raised:

FileNotFoundError: [Errno 2] No such file or directory: 'config.json'

what is a solution for this?

here is a screenshot of the code and everything.

Park
  • 2,446
  • 1
  • 16
  • 25
tareq
  • 11
  • 4

2 Answers2

2

the file config.jason needs to be in your current working directory to be found by open if you do not use a full path. Your cwd is probably where you launched python. To check do

    import os
    print(os.getcwd())

Your easiest solution is probably to use a full path name for the file.

William
  • 324
  • 1
  • 8
0

As shown in the screenshot of the IDE, you should exactly point out where the .json file. Now the json file and python code are not located in the same folder.

FYI, there are two types of path, absolute path and relative path. Please see https://www.kite.com/python/answers/how-to-find-the-absolute-and-relative-path-of-a-file-in-pythonhttps://www.kite.com/python/answers/how-to-find-the-absolute-and-relative-path-of-a-file-in-python

with open("/file program/config.json", "r") as f
    content = f.read()
    # do something

Or, just move out the config.json file to the folder, which the python code is located.

Park
  • 2,446
  • 1
  • 16
  • 25
  • I did what you say to put the .json file in the same folder as where python code is located like this https://imgur.com/a/9ulb9OU and now I get this issue in my terminal https://imgur.com/a/W9Te01U – tareq Feb 10 '22 at 03:27
  • Where is `bot.py`? The image showes that there is no `bot.py` at C:\Users\97154\Desktop\EinsteinBot-master (1)\EinsteinBot-master. – Park Feb 10 '22 at 04:01
  • I have it here.. https://imgur.com/a/9ulb9OU ... whats the problem then? – tareq Feb 10 '22 at 05:29
  • @tareq Now it is good to locate the two file in a same folder. Then, you have to run the bot.py at `C:\Users\97154\Desktop\EinsteinBot-master (1)\EinsteinBot-master\file program` because you move `bot.py` to the folder. – Park Feb 11 '22 at 00:43
  • yes I did it, I had to do cd "filename" I did it here and now Im having this cogs issue https://imgur.com/a/q0Q47Ec (I changed "EinsteinBot-master" file name to "chegg" ) here is the code for cogs https://imgur.com/a/O9qrAg2 – tareq Feb 11 '22 at 00:51
  • @tareq That is because you moved the `bot.py` to another folder so your code cannot find the `.cogs` path in the new folder. It is very necessary to understand a concept of `absolute path` and `relative path` (https://stackoverflow.com/questions/44772007/when-to-use-absolute-path-vs-relative-path-in-python), then you will realize why the issue happens. :) – Park Feb 11 '22 at 00:56
  • @tareq did you solve the issue? – Park Feb 12 '22 at 08:30