1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to get the parent dir location

Discussão em 'Python' iniciado por Stack, Novembro 26, 2025.

  1. Stack

    Stack Membro Participativo

    this code is get the templates/blog1/page.html in b.py:

    path = os.path.join(os.path.dirname(__file__), os.path.join('templates', 'blog1/page.html'))


    but i want to get the parent dir location:

    aParent
    |--a
    | |---b.py
    | |---templates
    | |--------blog1
    | |-------page.html
    |--templates
    |--------blog1
    |-------page.html


    and how to get the aParent location

    thanks

    updated:

    this is right:

    dirname=os.path.dirname
    path = os.path.join(dirname(dirname(__file__)), os.path.join('templates', 'blog1/page.html'))


    or

    path = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))

    Continue reading...

Compartilhe esta Página