我尝试使用以下命令在 python 上导入 json 文件:
with open('C:/Users/angel/message.json', 'r') as file:
data = json.load(file)
我有错误:
FileNotFoundError Traceback (most recent call last)
Cell In[1], line 1
----> 1 with open('C:/Users/angel/message.json', 'r') as file:
2 data = json.load(file)
File /lib/python3.12/site-packages/IPython/core/interactiveshell.py:324, in _modified_open(file, *args, **kwargs)
317 if file in {0, 1, 2}:
318 raise ValueError(
319 f"IPython won't let you open fd={file} by default "
320 "as it is likely to crash IPython. If you know what you are doing, "
321 "you can use builtins' open."
322 )
--> 324 return io_open(file, *args, **kwargs)
FileNotFoundError: [Errno 44] No such file or directory: 'C:/Users/angel/message.json'
我尝试将 json 文件放在不同的文件夹中,并更改“/”,但仍然收到错误。 你能帮忙吗?我不明白这个问题
尝试将路径写为r'C:\Users ngel\message.json'。如果这没有帮助,如果您将该文件夹放在与 python 脚本相同的目录中并尝试以“message.json”打开它会发生什么?
使用
os
声明json文件的路径
import os
path = os.path.join("C:","Users","angel","message.json")
您还可以使用
os.getcwd()
获取代码运行的当前路径
然后像这样在
path
中使用open
with open(path, 'r') as file: