Search code examples
pythonjsontracebackfile-not-found

Why doesn't Python3 fine a json-file which is in the same directory when I use the json.load() method


I'm learning Python3 and I'm trying to create an object Agent (a custom object) by initiating the attributes of it from a JSON file.

The problem is that when I launch my python file, it does not find the file, which is in the same directory. I checked the name and the is no typo. I don't understand where the problem really is.

Here is my folder structure:

project/
    model.py
    agents-100k.json

Here is my model.py file

import json


class Agent:
    def __init__(self, **agent_attributes):
        """Constructor of Agent class"""

        # Print each element of dict
        print(agent_attributes.items())

        # Get the name and the value of each entry in dict
        for attr_name, attr_value in agent_attributes.items():
            # setattr(instance, attribute_name, attribute_value)
            setattr(self, attr_name, attr_value)

    def say_hello(self, first_name):
        """Say hello to name given in argument"""

        return "Hello " + first_name + "!"


def main():
    for agent_attributes in json.load(open("agents-100k.json")):
        agent = Agent(**agent_attributes)
        print(agent.agreeableness)

main()

Here is a sample of the agents-100k.json file (there are a lot of entries, so I will just show two of them):

[
  {
    "age": 84,
    "agreeableness": -0.8437190198916452,
    "conscientiousness": 0.6271643010309115,
    "country_name": "China",
    "country_tld": "cn",
    "date_of_birth": "1933-12-27",
    "extraversion": 0.3229563709288293,
    "id": 227417393,
    "id_str": "bNn-9Gc",
    "income": 9881,
    "internet": false,
    "language": "Standard Chinese or Mandarin",
    "latitude": 33.15219798270325,
    "longitude": 100.85840672174572,
    "neuroticism": 0.15407262417068612,
    "openness": 0.041970542572878806,
    "religion": "unaffiliated",
    "sex": "Male"
  },
  {
    "age": 6,
    "agreeableness": -0.40747441203817747,
    "conscientiousness": 0.4352286422343134,
    "country_name": "Haiti",
    "country_tld": "ht",
    "date_of_birth": "2011-12-21",
    "extraversion": 1.4714618156987345,
    "id": 6821129477,
    "id_str": "bt3-xj9",
    "income": 1386,
    "internet": false,
    "language": "Creole",
    "latitude": 19.325567983697297,
    "longitude": -72.43795260265814,
    "neuroticism": -0.4503674752682471,
    "openness": -0.879092424231703,
    "religion": "Protestant",
    "sex": "Female"
  },
...
]

And finally, this is the error I get when I run python3 project/model.py:

Traceback (most recent call last):
  File "project/model.py", line 50, in <module>
    for agent_attributes in json.load(open("agents-100k.json")):
IOError: [Errno 2] No such file or directory: 'agents-100k.json'

Is there something I did wrong ?

Thanks for your help anyway.


Solution

  • Python opens the file relative to where the script is executed. So if you run the file with project/model.py the json should be outside of the project folder.

    If the json is always included on the same folder that your python file you can use the follow code to open the file:

    import json
    import os
    
    path = os.path.dirname(os.path.abspath(__file__))
    import jso
    
    
    def main():
        for agent_attributes in json.load(open(os.path.join(path, "agents-100k.json")):
            agent = Agent(**agent_attributes)
            print(agent.agreeableness)
    
    main()
    

    This question gives a more detailed explanation on how it works.