i know we can make us own function for do this with several os.path functions. but if there is a any existing function for do this, i want to use it
get_parent_dir("/path/to/myapp/modules") # returns => '/path/to/myapp/'
my solution is
parentdir = lambda path: os.path.realpath(path + "/.." )
Pathlib is the True Savior of All Things Path:
>>> import pathlib as plib
>>> x = plib.Path("/path/to/myapp/modules")
>>> print(x.parent)
\path\to\myapp
>>> x.parent
WindowsPath('/path/to/myapp')
You can the parent of a function with inspect:
This combined with Nilamo's code could be the start of a useful way to create program flow graphically.
import inspect
def function1():
print('Function1 called from: {}'.format(inspect.stack()[1][3]))
def function3():
print('Function1 called from: {}'.format(inspect.stack()[1][3]))
function1()
def function2():
print('Function2 called from: {}'.format(inspect.stack()[1][3]))
function3()
def main():
function2()
if __name__ == '__main__':
main()
results:
Output:
Function2 called from: main
Function1 called from: function2
Function1 called from: function3
Process finished with exit code 0