Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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")
over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

Yes there is:

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

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

over 4 years ago · Santiago Trujillo Report

0

You can use something like this:

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

Documentation:

pathlib.Path.joinpath

over 4 years ago · Santiago Trujillo Report

0

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)
over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!