Get absolute path of django app

后端 未结 6 2022
一整个雨季
一整个雨季 2021-02-03 20:09

I am writing a unit test that needs to access an image file that I put in \"fixtures\" directory right under my django app directory. I want to open up this image file in my tes

6条回答
  •  走了就别回头了
    2021-02-03 20:48

    Python3.4 and above comes with standard library pathlib.

    from pathlib import Path
    import appmodule
    
    pth = Path(appmodule.__file__).parent / 'fixtures'
    if pth.exists():
         "Your code here"
    

    parent will give you the path of your app directory, and / will append fixtures as path.

提交回复
热议问题