1

I simply wanted to read a JSON file using this code:

import json

with open("file.json") as File:
    print(json.load(File.read()))

When I try to run it using the VSCode debugger, I get the error:

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

But when I run it from the Terminal using python file.py it works. The problem is, that VSCode somehow uses an other "Working Directory" because when I run os.getcwd() in VSCode, I get the path to the parent folder of the folder the python script is in. When I run it from the Terminal, I get the right path. Why is that?

Just to point out: the problem is NOT the print statement/json. The same error shows up when I only try to open the file without anything else and then pass.

6
  • 2
    Whatever directory you're checking for file.json is not the directory from which Python is being run. Commented Jul 8, 2019 at 12:03
  • 2
    Try printing this to see what your current working directory is. Commented Jul 8, 2019 at 12:05
  • 1
    Just use an absolute path to the file you want to import. Relative paths are always dangerous when scripting. Commented Jul 8, 2019 at 12:05
  • I cannot reproduce the problem, and no: you surely don't have any file.json in the working directory. Commented Jul 8, 2019 at 12:05
  • 1
    Also note that you can load the file directly with json.load(File), instead json.loads(). Commented Jul 8, 2019 at 12:07

4 Answers 4

2
import os 
dir_path = os.getcwd()

Use this to get your current directory. You'll be able to discern where Python is running from. You can also use the full filepath for your JSON file.

import json

with open("fullpath/to/json/file/file.json") as File:
    print(json.loads(File.read()))
Sign up to request clarification or add additional context in comments.

5 Comments

Somehow, os.getcwd doesn't return the current path but rather the parent folder of the folder the file is in. I don't know why this is?
You get the path from where you called the script. If you want the path of the script, you can use this: from pathlib import Path and Path(__file__).resolve().parent
'os.getcwd doesn't return the current path', what do you mean by 'current path'?? @byJames
@BlueRineS os.getcwd() doesn't return the path of the folder the python file is in, but rather the parent folder of this folder.
@byJames To make the problem clearer, I would request you to edit your question by adding the project structure. Example: /ProjectFolder/--main.py; --file.json, --childFolder/--childFile.txt, etc.
0

As discussed in comments, your problem turned out to be the environment you were working in. When the program was executed from terminal, it worked and found the file. That is most likely because of the way your virtual environment is set up in VS Code. A virtual environment or venv as it is called, is an isolated environment of Python interpreter separate from your global Python install. It is useful when you are working on two different projects that require different versions of libraries. For example; a project that uses Django 1.10 and one that uses 1.9, so you don't have to keep shuffling between installing and uninstalling them.

A virtual environment is a directory tree which contains Python executable files and other files which indicate that it is a virtual environment.

As explained here, your .json file was most likely outside of your project virtual environment, and that is why it could not find it. I hope that helps you understand it.

6 Comments

Yes, it is in the same directory, but the problem isn't the json statement. Please read my question again, I updated it.
I don't think this might be source of the problem, but are you executing the script from cmd/terminal or from an IDE?
Thanks, I just noticed what the problem is: I executed it with VSCode, which somehow throws this error, but when I tried running it from the terminal, it worked! But why?
Because most IDEs run the code in virtual environment. The way your project is set up in the venv, it looks for the file in virtual environment instead of local directory. Someone else can probably give a better explaination than me as I am very bad at explaining stuff. :)
I don't think you are bad at explaning something because I only noticed the problem with this comment and now I kinda understand it, but it still seems confusing to me
|
0

You can verify if a file exists using os package:

import os.path
print(os.path.isfile("file.json"))

this should print True if the file exists.

Also you can try using the absolute path to make sure.

Comments

0
import  json


with open('/Users/my_pc/Downloads/example_2.json') as f:
     data = json.load(f)

print(data)

you can load your json by this way and give exact path of your directory where your json file is present.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.