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

Yes there is:

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

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


You can use something like this:

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

Documentation:

pathlib.Path.joinpath


Is the following definition of filepath closer in spirit to os.path.join?

import pathlib
main_dir = 'my_main_dir'
sub_dir = 'sub_dir'
fname = 'filename.tsv'
filepath = pathlib.PurePath(main_dir, sub_dir, fname)

You can simply join Path objects and strings:

    import pathlib
    script_parent_path = pathlib.Path(__file__).parent
    my_dir = ".env"
    my_new_path = pathlib.Path(script_parent_path, my_dir)
    print(my_new_path)

That's because:

Pathlib's constructors accept pathsegments. Each element of pathsegments can be either a string representing a path segment, an object implementing the os.PathLike interface which returns a string, or another path object - https://docs.python.org/3/library/pathlib.html#pathlib.PurePath