How to get the parent dir location

前端 未结 11 1982
鱼传尺愫
鱼传尺愫 2020-12-07 10:34

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\'))
         


        
11条回答
  •  隐瞒了意图╮
    2020-12-07 11:22

    Use relative path with the pathlib module in Python 3.4+:

    from pathlib import Path
    
    Path(__file__).parent
    

    You can use multiple calls to parent to go further in the path:

    Path(__file__).parent.parent
    

    As an alternative to specifying parent twice, you can use:

    Path(__file__).parents[1]
    

提交回复
热议问题