I am trying to the get the path of the directory of the current running file. For example, if I the following files: X/foo.py
and Y/bar.py
, and I am running python3 foo.py
and foo.py
imports bar.py
, and in bar.py
I get the parent directory, I want it to be Y and not X. How do I do that? currently, I get X with path = pathlib.Path(__file__).parent.resolve()
Asked
Active
Viewed 2,157 times
0

Ismael Padilla
- 5,246
- 4
- 23
- 35

Stack Overflow
- 377
- 4
- 16
-
`bar.__file__`, or if you're in `bar`, just `__file__` – Peter Wood Sep 05 '21 at 11:34
1 Answers
0
This question already was answered here: Find the current directory and file's directory
What you need to do is the following:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
This will return the absolute path to the current py-file.

Dominik Lovetinsky
- 466
- 5
- 15
-
Please read [answer] and do not write answers for questions that "were already answered" somewhere else. – Karl Knechtel Mar 15 '23 at 07:56