Search code examples
pythonpython-3.xpathlib

Is there a Pathlib alternate for os.path.join?


I am currently accessing the parent directory of my file using Pathlib as follows:

Path(__file__).parent

When I print it, and this gives me the following output:

print('Parent: ', Path(__file__).parent)
#output
/home/user/EC/main-folder

The main-folder has a .env file which I want to access and for that I want to join the parent path with the .env. Right now, I did:

dotenv_path = os.path.join(Path(__file__).parent, ".env")

which works. But I would like to know, if there is a Pathlib alternate to os.path.join()? Something like:

dotenv_path = pathlib_alternate_for_join(Path(__file__).parent, ".env")

Solution

  • Use pathlib.Path.joinpath:

    (Path(__file__).parent).joinpath('.env')