I am writing a piece of Python code, which requires reading a file from a relative position of the script itself.
This is the folder structure:
.
+-- cache
| +-- ..
| +-- population.json
+-- src
| +-- ..
| +-- script.py
+-- ..
I tried something like this:
folder = os.path.dirname(os.path.realpath("__file__"))
path = os.path.join(folder, "..{0}cache{1}population.json".format(os.path.sep, os.path.sep))
with open(path) as f:
population = load(f)
The problem is that folder is always set to the current folder which I am calling the script from.
So how can I fix it, in order to read the files independently of where I call the script from?