Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: open a json file in a different directory?

Tags:

python

json

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. However, I'm trying to get the json file from a different directory. 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

like image 581
InfinteScroll Avatar asked Oct 27 '25 23:10

InfinteScroll


1 Answers

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 /

like image 95
mhlester Avatar answered Oct 30 '25 17:10

mhlester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!