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
  How can I print from within actor(func) ?? Pedroski55 2 1,420 May-01-2024, 05:35 AM
Last Post: Pedroski55
  write code that resides in parent directory franklin97355 3 2,209 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 3,543 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Func Animation not displaying my live graph jotalo 0 2,316 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 7,384 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  Getter/Setter : get parent attribute, but no Getter/Setter in parent nboweb 2 5,302 May-11-2020, 07:22 PM
Last Post: nboweb
  call func from dict mcmxl22 3 4,290 Jun-21-2019, 05:20 AM
Last Post: snippsat
  About [from FILE import FUNC] Nwb 7 6,000 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 3,494 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 8,229 Jan-11-2019, 04:01 PM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.