0

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

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • Does this answer your question? [How do I get the full path of the current file's directory?](https://stackoverflow.com/questions/3430372/how-do-i-get-the-full-path-of-the-current-files-directory) – Pierre V. Dec 23 '19 at 13:06
  • What relative path did you try? If mammals is in the same folder as Zoo.py then the path from Zoo.py should be mammals/wolfname.txt – nicomp Dec 23 '19 at 13:08
  • @PierreV. `import os os.path.dirname(os.path.abspath(__file__))` would give me location of Zoo, wouldn't it? `C://.../ProjectRoot/` (I'd also want to run this program on Linux in the future). However, I'd have to hardcode something like `if s.endsWith("ProjectRoot"): go to mammals`? Then if I'd like to use `Wolf` in 10 other modules each located in different folder... Then what? – parsecer Dec 23 '19 at 13:09

3 Answers3

1

Instead of that; do this:

from mammals import Wolf

Then you can access Wolf class by Wolf.Wolf

Vicrobot
  • 3,795
  • 1
  • 17
  • 31
  • 1
    Thank you, this worked: `import mammals` and later `wolf = Wolf.Wolf()` – parsecer Dec 23 '19 at 13:15
  • @parsecer I just accessed a module from package that was in my current directory. See more at https://stackoverflow.com/a/7948504/9134528 – Vicrobot Dec 23 '19 at 13:17
  • Sorry, it didn't work... Tried bot `wolf = Wolf.Wolf()` and `wolf = Wolf()` – parsecer Dec 23 '19 at 13:32
  • @parsecer `from mammals.Wolf import Wolf ` will work too. If doesn't, there must be some other issue. – Vicrobot Dec 23 '19 at 13:34
  • The package does get accessed. If I make `print("inside Wolf")` the first line in `Wolf.py` then when running from `Zoo` it would print it, but then, when it tries to `wolf = Wolf()` it would print `FileNotFoundError: [Errno 2] No such file or directory: 'wolfname.txt' ` – parsecer Dec 23 '19 at 13:36
  • @parsecer The error is saying that it couldn't found the file 'wolfname.txt'. Try this: 'mammals/wolfname.txt' – Vicrobot Dec 23 '19 at 13:36
  • Then it works from inside `Zoo.py` but not from inside `Wolf.py`. And if I create a folder `house` and put `HousePets.py` inside and try to `wolf = Wolf()` it wouldn't find the file either. – parsecer Dec 23 '19 at 13:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204733/discussion-between-vicrobot-and-parsecer). – Vicrobot Dec 23 '19 at 13:39
0

Try renaming the 'wolf.py' file or Wolf classname into something else.

It will maybe solve the FileNotFoundError because the names are the same.

Otherwise: maybe this answer will help you out?

JaFizz
  • 328
  • 2
  • 20
0

I resorted to using this:

class Wolf:
    def __init__(self, path = ''):
        with open(path + 'wolfname.txt', 'r') as fileobj:
            self.name = fileobj.read()
        print(self.name)

and in Zoo:

from mammals import Wolf

print("inside Main.py")
wolf = Wolf.Wolf("mammals/")

Thus in different files I can pass different relative paths.

parsecer
  • 4,758
  • 13
  • 71
  • 140