Say I have two files in different directories: 1.py
(say, in C:/FIRST_FOLDER/1.py
) and 2.py
(say, in C:/SECOND_FOLDER/2.py
).
The file 1.py
imports 2.py
(using sys.path.insert(0, #path_of_2.py)
followed, obviously, by import 2
) and calls one of the functions in 2.py
, the function it calls needs to know the absolute path of 1.py
(it looks for a file in that same directory).
I know there's inspect.stack()[1]
but this only returns the file name and not the path.
Any idea on how to implement this?
import inspect
import os
abs_path = os.path.abspath((inspect.stack()[0])[1])
directory_of_1py = os.path.dirname(abs_path)
for more information on the module os.path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With