I needed to create a relative path starting with the current directory as a "." dot
For example, in windows ".\envs\.some.env" or "./envs/.some.env" elsewhere
I wanted to do this using pathlib. A solution was found, but it has a kludgy replace statement. Is there a better way to do this using pathlib?
The usage was django-environ, and the goal was to support multiple env files. The working folder contained an envs folder with the multiple env files within that folder.
import environ
from pathlib import Path
import os
domain_env = Path.cwd()
dotdot = Path("../")
some_env = dotdot / "envs" / ".some.env"
envsome = environ.Env()
envsome.read_env(envsome.str(str(domain_env), str(some_env).replace("..", ".")))
print(str(some_env))
print(str(some_env).replace("..", "."))
dot = Path("./") # Path(".") gives the same result
some_env = dot / "envs" / ".some.env"
print(str(some_env))
On windows gives:
..\envs\.some.env
.\envs\.some.env
envs\.some.env