Python Forum
How to get parent directory from existing func not user func ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get parent directory from existing func not user func ?
#1
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 + "/.." )
Reply
#2
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')
Reply
#3
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
Reply
#4
thanks for answers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  write code that resides in parent directory franklin97355 3 379 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  How to output one value per request of the CSV and print it in another func? Student44 3 1,323 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Func Animation not displaying my live graph jotalo 0 1,555 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 4,099 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  Getter/Setter : get parent attribute, but no Getter/Setter in parent nboweb 2 2,966 May-11-2020, 07:22 PM
Last Post: nboweb
  call func from dict mcmxl22 3 2,856 Jun-21-2019, 05:20 AM
Last Post: snippsat
  About [from FILE import FUNC] Nwb 7 3,591 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 2,343 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 6,008 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  How can I return my list from a func? Mike Ru 3 3,085 Oct-22-2018, 01:15 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020