0

I have project in Pycharm, and I want to use json file that placed in this project.

How can I call it?

I use this one:

import json

file = open('~/PycharmProjects/Test/JSON_new.json')
x = json.load(file)

And received the following error:

FileNotFoundError: [Errno 2] No such file or directory: '~/PycharmProjects/Test/JSON_new.json'

But path is correct

EDIT: I understood what is the problem. Instead of json file txt was created (but I selected json). It creates txt files, maybe, someone knows hot to solve it? I can create only .py files directly. Other files no.

Is it correct if I create scratch json file and placed it in Scratches?

Vas23Vewe
  • 81
  • 2
  • 4
  • 15
  • By its path ? What do you want to do with ? This is not a question of doing it "in pycharm" but rather : how to open a file on my filesystem – azro May 28 '20 at 08:39
  • 1
    Does this answer your question? [Reading JSON from a file?](https://stackoverflow.com/questions/20199126/reading-json-from-a-file) – azro May 28 '20 at 08:39
  • No, edited the description. – Vas23Vewe May 29 '20 at 09:19
  • Not sure `~` is handled – azro May 29 '20 at 09:20
  • I tried also with absolute path, with same result. The problem that it creates txt files instead of json. I don't understand why. – Vas23Vewe May 29 '20 at 09:23
  • This can't. You can't eask to read a JSON and it creates a txt file^^ you may run the wrong or something else – azro May 29 '20 at 09:25
  • I perform: double-click on project title --> new File --> file name --> select JSON in file type list. And instead of json usual txt was created. No '.json' extension near file name. – Vas23Vewe May 31 '20 at 09:00

2 Answers2

2

You may like to use following path(In linux):

file = open('/home/<user>/PycharmProjects/Test/JSON_new.json')

Replace user with your username. You need to know the correct path to the file, for which you can user PWD command in terminal.

Abin Lakhanpal
  • 380
  • 2
  • 15
1

You can use json module for this. You can open the file in a seperate object and pass it to json.load if you have a JSON string use json.loads.

import json

file = open('/path/to/json/file.json')

file_opened = json.load(file)
Sahil
  • 1,387
  • 14
  • 41