#5
Inspired Python
>>> from pathlib import Path
>>> p = Path('/srv/inspiredpython/')
>>> hello_path = p.parent.joinpath('documents', 'hello.txt')
>>> print(hello_path)
'/srv/documents/hello.txt'
>>> print(hello_path.with_suffix('.md').with_stem('greetings'))
'/srv/documents/greetings.md'

Python Pathlib Tips: The “Builder” Pattern


pathlib’s Path class makes use of a programming paradigm known as the Builder pattern. When you access most properties or methods on a Path object, you are given a new Path object with the desired changes. That means you can safely chain operations together to build your desired path.

Be Inspired Get Python tips sent to your inbox

We'll tell you about the latest courses and articles.

Absolutely no spam. We promise!