[英]python: open a json file in a different directory?
Here's my code:这是我的代码:
import json
with open("json.items") as json_file:
json_data = json.load(json_file)
It works fine when I move the json file into the same directory.当我将 json 文件移动到同一目录时,它工作正常。 However, I'm trying to get the json file from a different directory.但是,我正在尝试从不同的目录中获取 json 文件。 How would I do that?我该怎么做? This is what I have tried and its not working:这是我尝试过但不起作用的方法:
with open("/lowerfolder/json.items") as json_file:
Any help?有什么帮助吗? Thanks谢谢
Depending on your platform, starting a path with /
means absolute path from the root根据您的平台,以/
开头的路径表示从根开始的绝对路径
Meaning a relative path should be open("lowerfolder/json.items")
without the /
意味着相对路径应该是open("lowerfolder/json.items")
没有/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.