【问题标题】:opening json file error : FileNotFoundError: [Errno 2] No such file or directory打开 json 文件错误:FileNotFoundError: [Errno 2] 没有这样的文件或目录
【发布时间】:2021-04-29 21:34:25
【问题描述】:

我是一个非常初学者的编程,我被这个“FileNotFoundError:[Errno 2]没有这样的文件或目录:”错误所困扰。我想加载一个与我的 python 脚本位于同一文件夹中的 JSON 文件。

with open("dictionnaire.json") as file:
data = json.load(file)

我正在使用 python 3.9.0,我不明白出了什么问题。请问可以吗?

谢谢

【问题讨论】:

  • 欢迎来到 SO。请发布完整的回溯错误。

标签: json python-3.x


【解决方案1】:

你必须像这样指定路径:

 with open("./path_to_json_file/data.json", 'r') as f:
      data = json.load(f)
      f.close()



for key, value in data.items():
   print("key: {} | value: {}".format(key, value))

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 2019-12-12
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多