简体   繁体   中英

How to use json file that placed in the python project?

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?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM