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

reinhardt picture reinhardt · Apr 20, 2020 · Viewed 8.5k times · Source

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")

Answer

quest picture quest · Apr 20, 2020

Yes there is:

env_path = Path(__file__).parent / ".env"

/ is all you need. This will work in different OSs