0

I'm trying to find the parent directory of the directory that my script is located in:

this = pathlib.Path(__name__)
parent = this.parent
parent2 = parent.parent

But printing them out shows that the second .parent isn't working:

print(this, this.absolute())
print(parent, parent.absolute())
print(parent2, parent2.absolute())
print(this.parent == this.parent.parent)

Yields the output of:

__main__ C:\Users\Markus\Projects\PathTest\bin\__main__
. C:\Users\Markus\Projects\PathTest\bin
. C:\Users\Markus\Projects\PathTest\bin
True

I'm clueless, what could be the issue?

Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
  • 1
    using `this = Path(__main__)` everything works as expected... what is `Parent`? – hiro protagonist May 15 '19 at 11:57
  • What is `print(__name__)`? If I run your code on MacOS, both `__name__` *and* this are just `'__main__'`, and it's parent is simply `.'` (and consequently, also the grandparent). There is no expansion to a full path in my case, but that may be different per platform . – 9769953 May 15 '19 at 12:02
  • `absolute` will join a relative path with the current working directory. – Peter Wood May 15 '19 at 12:04
  • @9769953 Yeah, running `print(__name__)` gives me `__main__`, but I don't understand why I can't run `.parent` twice on it, it knows the `absolute()` path well. – Markus Meskanen May 15 '19 at 12:04
  • Your last edit shows the problem: pathlib works on the relative directory name, which is not the absolute path name. Hence it gets "stuck" on the single . directory, and the absolute path also remains the same. – 9769953 May 15 '19 at 12:04
  • @9769953 I suppose I understand the problem, I'm just surprised that pathlib can't handle `..` for relative paths, even Python's import understands `from ...superparent import stuff`. Anyways, I'm still clueless on how to do this "properly" then – Markus Meskanen May 15 '19 at 12:11
  • I suspect that pathlib does as much as possible without performing system calls, for speed and ease. Thus, it works from the string provided, and the directory separator(s) for the system; in that case, it has a relative root which it won't go beyond. This is also the reason why `__name__` instead of `__file__` works. I guess only when methods such as resolve or absolute are called, the underlying system is being polled about its directory structure, or rather, the current location of the file/process its being used in. But this is just a guess; I haven't looked at the actual code. – 9769953 May 15 '19 at 12:17
  • @MarkusMeskanen I think what you're misunderstanding is that `pathlib` is for manipulating path *syntax*. The path doesn't have to actually exist. It's only when you deliberately resolve it that it will become meaningful in a context. Implicitly it resolves to be relative to the current working directory. – Peter Wood May 16 '19 at 07:26
  • @PeterWood You're right, that's something I certainly didn't realize when I first asked this question, thanks for the clarification. However, I still think that the parent of `.` should be `..`, and the parent of that should be `../..`, and I'm unable to see why pathlib doesn't work that way. – Markus Meskanen May 16 '19 at 09:23

1 Answers1

2

as the printout shows: if parent = '.' then parent.parent will also be ..

try to resolve the path beforehand:

this = Path(__file__).resolve()

also note that __file__ will give you the path of your file; not __main__.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111