Suppose I have a file structure like this one:
test
mammals
Wolf.py
wolfname.txt
Zoo.py
And inside Wolf.py
I have:
class Wolf:
def __init__(self):
with open('wolfname.txt', 'r') as fileobj:
self.name = fileobj.read()
print(self.name)
wolf = Wolf()
If I call it from inside Wolf.py
, it works fine. If I call it from inside Zoo.py
, it gives a FFileNotFoundError: [Errno 2] No such file or directory: 'wolfname.txt'
error.
What is the way to fix it without resolving to absolute paths? I might want to use the Wolf
class from some other new package in the future as well.
I use this import inside the Zoo:
from mammals.Wolf import Wolf
wolf = Wolf()
EDIT: I've made this repl.it to show it online: https://repl.it/repls/DrearyGrizzledMicroscope