Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add multiple elements to pathlib path

I have a Python pathlib Path and a list of strings, and I’d like to concatenate the strings to the path. This works

from pathlib import Path

a = Path("a")
lst = ["b", "c", "d"]

for item in lst:
    a = a / item

print(a)
a/b/c/d

but is a little clumsy. Can the for loop be replaced by something else?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This might be what you are after:

from pathlib import Path

a = Path("a")
dir_list = ["b", "c", "d"]

a = a.joinpath(*dir_list)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading