skip to Main Content

I try to import a json file on python with :

with open('C:/Users/angel/message.json', 'r') as file:
    data = json.load(file)

I have the error :

FileNotFoundError                         Traceback (most recent call last)
Cell In[1], line 1
----> 1 with open('C:/Users/angel/message.json', 'r') as file:
      2     data = json.load(file)

File /lib/python3.12/site-packages/IPython/core/interactiveshell.py:324, in _modified_open(file, *args, **kwargs)
    317 if file in {0, 1, 2}:
    318     raise ValueError(
    319         f"IPython won't let you open fd={file} by default "
    320         "as it is likely to crash IPython. If you know what you are doing, "
    321         "you can use builtins' open."
    322     )
--> 324 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 44] No such file or directory: 'C:/Users/angel/message.json'

I’ve tried putting my json file in different folders, and changing the “/” but I still get the error.
Can you help please ? I don’t understand the problem

2

Answers


  1. Try writing the path as r’C:Usersangelmessage.json’. If this doesn’t help, what happens if you put the folder in the same directory as the python script and try to open it just as ‘message.json’?

    Login or Signup to reply.
  2. use os for declaring the path of json file

    example

    import os
    path = os.path.join("C:","Users","angel","message.json")
    

    you can also use os.getcwd() to get current path your code is running

    and just use path in open like this

    with open(path, 'r') as file:
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search