简体   繁体   中英

Guys I need help to open a json file from python, but the json is located in another directory

I´m running my python file in a directory, let´s say: /python/python_file.py

and the json relative is: /python/python_apps_file/disctionary.json

and all I need to do is:

import json
import random
import os.path

with open(".\python_apps_file\english_dictionary.json","r") as dictionary:
    my_dictionary = list(json.load(dictionary).keys())

print(dictionary[5])

I even tried with:

with open(os.path.abspath("english_dictionary.json"),"r") as dictionary:
    my_dictionary = list(json.load(dictionary).keys())

but still getting the same error: FileNotFoundError: [Errno 2] No such file or directory:

In your example the filenames are different. It might be a spelling problem.

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